-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
50 lines (43 loc) · 1.49 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# coverage run --branch tests.py && coverage report -m
import inspect
import logging
import os
import pathlib
import sys
import unittest
import importlib.util
def imp_load_source ( module_name, path ):
#print ( f'loading {path!r}' )
spec = importlib.util.spec_from_file_location ( module_name, path )
module = importlib.util.module_from_spec ( spec )
spec.loader.exec_module ( module )
return module
logging.basicConfig (
stream = sys.stdout,
#level = logging.DEBUG,
format = (
#'%(asctime)s '
'[%(name)s %(levelname)s] '
'%(message)s'
),
)
loader = unittest.TestLoader()
suite = unittest.TestSuite()
def look_for_tests ( path, prefix='' ):
for p in pathlib.Path ( path ).glob ( '*_test.py' ):
module_name = prefix + os.path.splitext ( p.name )[0]
module = imp_load_source ( module_name, str ( p ) )
for attr in dir ( module ):
if attr[0] != '_':
x = getattr ( module, attr )
#print ( 'checking', attr )
if inspect.isclass ( x ) and issubclass ( x, unittest.TestCase ):
#print ( 'TestCase:', module_name, attr )
cls = getattr ( module, attr )
suite.addTest ( loader.loadTestsFromTestCase ( cls ) )
#else:
# print ( 'garbage:', module_name, attr )
look_for_tests ( 'tests', 'tests.' )
look_for_tests ( '.' )
#logging.basicConfig ( stream=sys.stdout, level=logging.DEBUG, format='%(asctime)s [%(threadName)s %(levelname)s] %(message)s' )
unittest.TextTestRunner ( verbosity = 1, failfast = True ).run ( suite )