1 | """Regression testing framework |
---|
2 | This module will search for scripts in the same directory named |
---|
3 | test_*.py. Each such script should be a test suite that tests a |
---|
4 | module through PyUnit. This script will aggregate all |
---|
5 | found test suites into one big test suite and run them all at once. |
---|
6 | """ |
---|
7 | |
---|
8 | # Author: Mark Pilgrim |
---|
9 | # Modified by Ole Nielsen |
---|
10 | |
---|
11 | import unittest |
---|
12 | import os |
---|
13 | import sys |
---|
14 | import tempfile |
---|
15 | |
---|
16 | |
---|
17 | #List files that should be excluded from the testing process. |
---|
18 | #E.g. if they are known to fail and under development |
---|
19 | |
---|
20 | exclude_files = [] |
---|
21 | |
---|
22 | #if sys.platform != 'win32': |
---|
23 | # exclude_files.append('test_advection.py') #Weave doesn't work on Linux |
---|
24 | |
---|
25 | # Exclude test_advection on all platforms for the time being. See ticket:205 |
---|
26 | #exclude_files.append('test_advection.py') #Weave doesn't work on Linux |
---|
27 | |
---|
28 | |
---|
29 | # Directories that should not be searched for test files. |
---|
30 | exclude_dirs = ['pypar_dist', #Special requirements |
---|
31 | 'props', 'wcprops', 'prop-base', 'text-base', '.svn', #Svn |
---|
32 | 'tmp'] |
---|
33 | |
---|
34 | |
---|
35 | print "The following directories will be skipped over;" |
---|
36 | for dir in exclude_dirs: |
---|
37 | print dir |
---|
38 | print "" |
---|
39 | |
---|
40 | def get_test_files(path): |
---|
41 | |
---|
42 | |
---|
43 | try: |
---|
44 | files = os.listdir(path) |
---|
45 | except: |
---|
46 | return [] |
---|
47 | |
---|
48 | #Check sub directories |
---|
49 | test_files = [] |
---|
50 | |
---|
51 | #Exclude svn admin dirs |
---|
52 | files = [x for x in files if x not in exclude_dirs] |
---|
53 | path_files = [] |
---|
54 | for file in files: |
---|
55 | |
---|
56 | absolute_filename = path + os.sep + file |
---|
57 | |
---|
58 | #sys.path.append('pmesh') |
---|
59 | if os.path.isdir(absolute_filename): |
---|
60 | sys.path.append(file) #FIXME: May cause name conflicts between pyvolution\mesh.py and pmesh\mesh.py on some systems |
---|
61 | path_files.append(file) |
---|
62 | print file + ',', |
---|
63 | more_test_files, more_path_files =\ |
---|
64 | get_test_files(absolute_filename) |
---|
65 | |
---|
66 | test_files += more_test_files |
---|
67 | path_files += more_path_files |
---|
68 | elif file.startswith('test_') and file.endswith('.py'): |
---|
69 | test_files.append(file) |
---|
70 | else: |
---|
71 | pass |
---|
72 | |
---|
73 | return test_files, path_files |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | def regressionTest(test_verbose=False): |
---|
78 | path = os.getcwd() |
---|
79 | print 'Recursing into;' |
---|
80 | test_files, path_files = get_test_files(path) |
---|
81 | |
---|
82 | files = [x for x in test_files if not x == 'test_all.py'] |
---|
83 | |
---|
84 | files.sort() # Ensure same order on all platforms |
---|
85 | |
---|
86 | print |
---|
87 | print |
---|
88 | print 'Testing path %s:' %('...'+path[-50:]) |
---|
89 | print |
---|
90 | print 'Files tested;' |
---|
91 | #print_files = [] |
---|
92 | for file in files: |
---|
93 | #print_files += file + ' ' |
---|
94 | print file + ',', |
---|
95 | print |
---|
96 | print |
---|
97 | if globals().has_key('exclude_files'): |
---|
98 | for file in exclude_files: |
---|
99 | print 'WARNING: File '+ file + ' to be excluded from testing' |
---|
100 | try: |
---|
101 | files.remove(file) |
---|
102 | except ValueError, e: |
---|
103 | msg = 'File "%s" was not found in test suite.\n' %file |
---|
104 | msg += 'Original error is "%s"\n' %e |
---|
105 | msg += 'Perhaps it should be removed from exclude list?' |
---|
106 | raise Exception, msg |
---|
107 | |
---|
108 | filenameToModuleName = lambda f: os.path.splitext(f)[0] |
---|
109 | moduleNames = map(filenameToModuleName, files) |
---|
110 | modules = map(__import__, moduleNames) |
---|
111 | |
---|
112 | # Fix up the system path |
---|
113 | for file in path_files: |
---|
114 | sys.path.remove(file) |
---|
115 | |
---|
116 | load = unittest.defaultTestLoader.loadTestsFromModule |
---|
117 | testCaseClasses = map(load, modules) |
---|
118 | |
---|
119 | |
---|
120 | if test_verbose is True: |
---|
121 | # Test the code by setting verbose to True. |
---|
122 | # The test cases have to be set up for this to work. |
---|
123 | # See test data manager for an example. |
---|
124 | for test_suite in testCaseClasses: |
---|
125 | for tests in test_suite._tests: |
---|
126 | # tests is of class TestSuite |
---|
127 | if len(tests._tests) > 1: |
---|
128 | # these are the test functions |
---|
129 | try: |
---|
130 | # Calls class method set_verbose in the test case classes |
---|
131 | # print 'Tests', tests._tests[0] |
---|
132 | # print 'Type', type(tests._tests[0]) |
---|
133 | tests._tests[0].set_verbose() |
---|
134 | except: |
---|
135 | pass # No all classes have set_verbose |
---|
136 | return unittest.TestSuite(testCaseClasses) |
---|
137 | |
---|
138 | def check_anuga_import(): |
---|
139 | try: |
---|
140 | # importing something that loads quickly |
---|
141 | import anuga.utilities.anuga_exceptions |
---|
142 | except ImportError: |
---|
143 | print "Python cannot import ANUGA module." |
---|
144 | print "Check you have followed all steps of its installation." |
---|
145 | import sys; sys.exit() |
---|
146 | |
---|
147 | |
---|
148 | if __name__ == '__main__': |
---|
149 | check_anuga_import() |
---|
150 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
151 | test_verbose = True |
---|
152 | saveout = sys.stdout |
---|
153 | filename = ".temp" |
---|
154 | fid = open(filename, 'w') |
---|
155 | sys.stdout = fid |
---|
156 | else: |
---|
157 | test_verbose = False |
---|
158 | suite = regressionTest(test_verbose) |
---|
159 | runner = unittest.TextTestRunner() #verbosity=2 |
---|
160 | runner.run(suite) |
---|
161 | |
---|
162 | # Cleaning up |
---|
163 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
164 | sys.stdout = saveout |
---|
165 | #fid.close() # This was causing an error in windows |
---|
166 | #os.remove(filename) |
---|
167 | |
---|