forked from hegland/cmepy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_all.py
99 lines (80 loc) · 2.42 KB
/
test_all.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
hook allowing all unit tests to be run via setuptools
"""
import unittest
# root package to gather unit tests from
ROOT_PACKAGE = 'cmepy'
# package structure, leaves must contain suite() method that returns
# the test suite for the sub package
SUB_PACKAGES = {
ROOT_PACKAGE : [
'tests',
],
'tests' : [
'ode_solver_tests',
'solver_tests',
'recorder_tests',
'domain_tests',
'state_enum_tests',
'lexarrayset_tests',
'statistics_tests',
'measurement_tests',
'model_tests',
],
}
def import_sub_package(sub_package_chain):
"""
import_sub_package(['A', 'B', 'C']) -> module
where module is the sub package A.B.C
"""
name = '.'.join(sub_package_chain)
fromlist = [sub_package_chain[-1]]
return __import__(name, fromlist = fromlist)
def gather_suites(sub_packages, root_package):
"""
gather_suites(sub_packages, root_package) -> set of unittest.TestSuite
"""
test_suites = list()
def dfs_add_tests(chain):
"""
dfs_add_tests(chain)
recursively explore package structure using dfs, loading
leaf sub packages, and adding the results of their suite()
method to the set test_suites.
"""
head = chain[-1]
if head not in sub_packages:
sub_package = import_sub_package(chain)
print '\t+ %s' % str(sub_package)
try:
suite = sub_package.suite()
test_suites.append(suite)
except AttributeError:
detail = 'sub package \'%s\' has no \'suite()\' method' % head
print '\t -- WARNING : %s, ignoring' % detail
else:
for sub_package in sub_packages[head]:
dfs_add_tests(chain + [sub_package])
dfs_add_tests([root_package])
return test_suites
def additional_tests():
"""
additional_tests() -> unittest.TestSuite
"""
print ''
print '-- gathering test suites :'
print ''
test_suite = gather_suites(SUB_PACKAGES, ROOT_PACKAGE)
print ''
print '-- running test suites :'
print ''
all_test_suite = unittest.TestSuite(test_suite)
return all_test_suite
def main():
"""
gathers and runs all the tests
"""
suite = additional_tests()
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
main()