[3464] | 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 |
---|
[4414] | 14 | import tempfile |
---|
[3464] | 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 | |
---|
[3573] | 20 | exclude_files = [] |
---|
[4796] | 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 |
---|
[4978] | 26 | #exclude_files.append('test_advection.py') #Weave doesn't work on Linux |
---|
[4796] | 27 | |
---|
| 28 | |
---|
| 29 | # Directories that should not be searched for test files. |
---|
[3464] | 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) |
---|
[4461] | 62 | print file + ',', |
---|
[4804] | 63 | more_test_files, more_path_files =\ |
---|
| 64 | get_test_files(absolute_filename) |
---|
| 65 | |
---|
[3464] | 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 |
---|
[4804] | 72 | |
---|
| 73 | return test_files, path_files |
---|
[3464] | 74 | |
---|
| 75 | |
---|
| 76 | |
---|
[4414] | 77 | def regressionTest(test_verbose=False): |
---|
[3464] | 78 | path = os.getcwd() |
---|
[4461] | 79 | print 'Recursing into;' |
---|
[3464] | 80 | test_files, path_files = get_test_files(path) |
---|
[4804] | 81 | |
---|
[3464] | 82 | files = [x for x in test_files if not x == 'test_all.py'] |
---|
[4804] | 83 | |
---|
| 84 | files.sort() # Ensure same order on all platforms |
---|
| 85 | |
---|
[4461] | 86 | print |
---|
| 87 | print |
---|
[3464] | 88 | print 'Testing path %s:' %('...'+path[-50:]) |
---|
[4461] | 89 | print |
---|
| 90 | print 'Files tested;' |
---|
| 91 | #print_files = [] |
---|
[3464] | 92 | for file in files: |
---|
[4461] | 93 | #print_files += file + ' ' |
---|
| 94 | print file + ',', |
---|
| 95 | print |
---|
| 96 | print |
---|
[3464] | 97 | if globals().has_key('exclude_files'): |
---|
| 98 | for file in exclude_files: |
---|
| 99 | print 'WARNING: File '+ file + ' to be excluded from testing' |
---|
[4795] | 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 |
---|
[3464] | 107 | |
---|
| 108 | filenameToModuleName = lambda f: os.path.splitext(f)[0] |
---|
| 109 | moduleNames = map(filenameToModuleName, files) |
---|
| 110 | modules = map(__import__, moduleNames) |
---|
[4804] | 111 | |
---|
[3464] | 112 | # Fix up the system path |
---|
| 113 | for file in path_files: |
---|
| 114 | sys.path.remove(file) |
---|
[4804] | 115 | |
---|
[3464] | 116 | load = unittest.defaultTestLoader.loadTestsFromModule |
---|
[4414] | 117 | testCaseClasses = map(load, modules) |
---|
[4418] | 118 | |
---|
| 119 | |
---|
[4414] | 120 | if test_verbose is True: |
---|
[4415] | 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. |
---|
[4414] | 124 | for test_suite in testCaseClasses: |
---|
| 125 | for tests in test_suite._tests: |
---|
[4418] | 126 | # tests is of class TestSuite |
---|
| 127 | if len(tests._tests) > 1: |
---|
[4414] | 128 | # these are the test functions |
---|
| 129 | try: |
---|
[4418] | 130 | # Calls class method set_verbose in the test case classes |
---|
| 131 | # print 'Tests', tests._tests[0] |
---|
| 132 | # print 'Type', type(tests._tests[0]) |
---|
[4414] | 133 | tests._tests[0].set_verbose() |
---|
| 134 | except: |
---|
[4415] | 135 | pass # No all classes have set_verbose |
---|
[4414] | 136 | return unittest.TestSuite(testCaseClasses) |
---|
[3464] | 137 | |
---|
[4763] | 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 | |
---|
[3464] | 148 | if __name__ == '__main__': |
---|
[4763] | 149 | check_anuga_import() |
---|
[4414] | 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) |
---|
[3464] | 159 | runner = unittest.TextTestRunner() #verbosity=2 |
---|
| 160 | runner.run(suite) |
---|
[3678] | 161 | |
---|
[4414] | 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 | |
---|