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 | if sys.platform != 'win32': #Windows |
---|
22 | exclude_files.append('test_advection.py') #Weave doesn't work on Linux |
---|
23 | |
---|
24 | exclude_dirs = ['pypar_dist', #Special requirements |
---|
25 | 'props', 'wcprops', 'prop-base', 'text-base', '.svn', #Svn |
---|
26 | 'tmp'] |
---|
27 | |
---|
28 | |
---|
29 | print "The following directories will be skipped over;" |
---|
30 | for dir in exclude_dirs: |
---|
31 | print dir |
---|
32 | print "" |
---|
33 | |
---|
34 | def get_test_files(path): |
---|
35 | |
---|
36 | |
---|
37 | try: |
---|
38 | files = os.listdir(path) |
---|
39 | except: |
---|
40 | return [] |
---|
41 | |
---|
42 | #Check sub directories |
---|
43 | test_files = [] |
---|
44 | |
---|
45 | #Exclude svn admin dirs |
---|
46 | files = [x for x in files if x not in exclude_dirs] |
---|
47 | path_files = [] |
---|
48 | for file in files: |
---|
49 | |
---|
50 | absolute_filename = path + os.sep + file |
---|
51 | |
---|
52 | #sys.path.append('pmesh') |
---|
53 | if os.path.isdir(absolute_filename): |
---|
54 | sys.path.append(file) #FIXME: May cause name conflicts between pyvolution\mesh.py and pmesh\mesh.py on some systems |
---|
55 | path_files.append(file) |
---|
56 | print file + ',', |
---|
57 | more_test_files, more_path_files =get_test_files(absolute_filename) |
---|
58 | test_files += more_test_files |
---|
59 | path_files += more_path_files |
---|
60 | elif file.startswith('test_') and file.endswith('.py'): |
---|
61 | test_files.append(file) |
---|
62 | else: |
---|
63 | pass |
---|
64 | return test_files , path_files |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | def regressionTest(test_verbose=False): |
---|
69 | path = os.getcwd() |
---|
70 | print 'Recursing into;' |
---|
71 | test_files, path_files = get_test_files(path) |
---|
72 | files = [x for x in test_files if not x == 'test_all.py'] |
---|
73 | print |
---|
74 | print |
---|
75 | print 'Testing path %s:' %('...'+path[-50:]) |
---|
76 | print |
---|
77 | print 'Files tested;' |
---|
78 | #print_files = [] |
---|
79 | for file in files: |
---|
80 | #print_files += file + ' ' |
---|
81 | print file + ',', |
---|
82 | print |
---|
83 | print |
---|
84 | if globals().has_key('exclude_files'): |
---|
85 | for file in exclude_files: |
---|
86 | print 'WARNING: File '+ file + ' to be excluded from testing' |
---|
87 | try: |
---|
88 | files.remove(file) |
---|
89 | except ValueError, e: |
---|
90 | msg = 'File "%s" was not found in test suite.\n' %file |
---|
91 | msg += 'Original error is "%s"\n' %e |
---|
92 | msg += 'Perhaps it should be removed from exclude list?' |
---|
93 | raise Exception, msg |
---|
94 | |
---|
95 | filenameToModuleName = lambda f: os.path.splitext(f)[0] |
---|
96 | moduleNames = map(filenameToModuleName, files) |
---|
97 | modules = map(__import__, moduleNames) |
---|
98 | # Fix up the system path |
---|
99 | for file in path_files: |
---|
100 | sys.path.remove(file) |
---|
101 | load = unittest.defaultTestLoader.loadTestsFromModule |
---|
102 | testCaseClasses = map(load, modules) |
---|
103 | |
---|
104 | |
---|
105 | if test_verbose is True: |
---|
106 | # Test the code by setting verbose to True. |
---|
107 | # The test cases have to be set up for this to work. |
---|
108 | # See test data manager for an example. |
---|
109 | for test_suite in testCaseClasses: |
---|
110 | for tests in test_suite._tests: |
---|
111 | # tests is of class TestSuite |
---|
112 | if len(tests._tests) > 1: |
---|
113 | # these are the test functions |
---|
114 | try: |
---|
115 | # Calls class method set_verbose in the test case classes |
---|
116 | # print 'Tests', tests._tests[0] |
---|
117 | # print 'Type', type(tests._tests[0]) |
---|
118 | tests._tests[0].set_verbose() |
---|
119 | except: |
---|
120 | pass # No all classes have set_verbose |
---|
121 | return unittest.TestSuite(testCaseClasses) |
---|
122 | |
---|
123 | if __name__ == '__main__': |
---|
124 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
125 | test_verbose = True |
---|
126 | saveout = sys.stdout |
---|
127 | filename = ".temp" |
---|
128 | fid = open(filename, 'w') |
---|
129 | sys.stdout = fid |
---|
130 | else: |
---|
131 | test_verbose = False |
---|
132 | suite = regressionTest(test_verbose) |
---|
133 | runner = unittest.TextTestRunner() #verbosity=2 |
---|
134 | runner.run(suite) |
---|
135 | |
---|
136 | # Cleaning up |
---|
137 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
138 | sys.stdout = saveout |
---|
139 | #fid.close() # This was causing an error in windows |
---|
140 | #os.remove(filename) |
---|
141 | |
---|