-
Notifications
You must be signed in to change notification settings - Fork 240
/
run_test.py
67 lines (49 loc) · 1.83 KB
/
run_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
This allows you to run a specific test function by itself from an importable module.
The function can be either a method of a unittest.TestCase subclass, or just a function defined
at module level. This is useful when running an individual test under mpirun for debugging
purposes.
To specify the test to run, use the following forms:
<modpath>:<testcase name>.<funcname> OR <modpath>:<funcname>
where <modpath> is either the dotted module name or the full filesystem path of the python file.
for example:
mpirun -n 4 run_test mypackage.mysubpackage.mymod:MyTestCase.test_foo
OR
mpirun -n 4 run_test /foo/bar/mypackage/mypackage/mysubpackage/mymod.py:MyTestCase.test_foo
"""
import sys
import importlib
from openmdao.utils.file_utils import get_module_path
def run_test():
"""
Run individual test(s).
"""
sys.path.append('.')
if len(sys.argv) > 1:
testspec = sys.argv[1]
parts = testspec.split(':')
if len(sys.argv) != 2 or len(parts) != 2:
print('Usage: run_test my_mod_path:my_test_case.test_func_name\n'
' OR\n'
' run_test my_mod_path:test_func_name')
sys.exit(-1)
modpath, funcpath = parts
if modpath.endswith('.py'):
modpath = get_module_path(modpath)
mod = importlib.import_module(modpath)
parts = funcpath.split('.', 1)
if len(parts) == 2:
tcase_name, method_name = parts
testcase = getattr(mod, tcase_name)(methodName=method_name)
setup = getattr(testcase, 'setUp', None)
if setup is not None:
setup()
getattr(testcase, method_name)()
teardown = getattr(testcase, 'tearDown', None)
if teardown:
teardown()
else:
funcname = parts[0]
getattr(mod, funcname)()
if __name__ == '__main__':
run_test()