[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 |
---|
[6220] | 15 | import time |
---|
| 16 | import anuga.utilities.system_tools as aust |
---|
[6910] | 17 | from anuga.utilities.terminal_width import terminal_width |
---|
[3464] | 18 | |
---|
| 19 | |
---|
| 20 | #List files that should be excluded from the testing process. |
---|
| 21 | #E.g. if they are known to fail and under development |
---|
[3573] | 22 | exclude_files = [] |
---|
[4796] | 23 | |
---|
[6086] | 24 | # Directories that should not be searched for test files. |
---|
[6218] | 25 | exclude_dirs = ['pypar_dist', # Special requirements |
---|
| 26 | '.svn', # subversion |
---|
| 27 | 'props', 'wcprops', 'prop-base', 'text-base', 'tmp'] |
---|
[4796] | 28 | |
---|
[3464] | 29 | |
---|
[6218] | 30 | ## |
---|
| 31 | # @brief List a string sequence on the screen in columns. |
---|
| 32 | # @param names Sequence of strings to list. |
---|
| 33 | # @param func Function to apply to each string in sequence. |
---|
| 34 | # @param col_width Force columns to this width (default calculated). |
---|
| 35 | # @param page_width Set displayable page width to this (default 132). |
---|
| 36 | def list_names(names, func=None, col_width=None, page_width=None): |
---|
| 37 | # set defaults |
---|
[6904] | 38 | p_width = page_width - 1 # set page width |
---|
[6218] | 39 | if p_width is None: |
---|
| 40 | p_width = 132 # default page width |
---|
[3464] | 41 | |
---|
[6218] | 42 | c_width = col_width # set column width |
---|
| 43 | if c_width is None: |
---|
| 44 | c_width = 0 |
---|
| 45 | for name in names: |
---|
| 46 | if func: |
---|
| 47 | name = func(name) |
---|
| 48 | c_width = max(c_width, len(name)) |
---|
[6904] | 49 | c_width += 2 # 2 column padding |
---|
[6086] | 50 | |
---|
[6218] | 51 | # calculate number of columns allowed |
---|
| 52 | max_columns = int(p_width / c_width) |
---|
| 53 | |
---|
| 54 | # print columns |
---|
| 55 | column = 0 |
---|
| 56 | for name in names: |
---|
| 57 | if func: |
---|
| 58 | name = func(name) |
---|
[6904] | 59 | print '%-*s' % (c_width-1, name), |
---|
[6218] | 60 | column += 1 |
---|
| 61 | if column >= max_columns: |
---|
| 62 | column = 0 |
---|
| 63 | print |
---|
| 64 | |
---|
| 65 | # if last line not finished, end it here |
---|
| 66 | if column > 0: |
---|
| 67 | print |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | ## |
---|
| 71 | # @brief Get 'test_*.py' files and paths to directories. |
---|
| 72 | # @param path Path to directory to start walk in. |
---|
| 73 | # @return A tuple (<files>, <dirs>). |
---|
| 74 | # @note Don't include any files in and below forbidden directories. |
---|
[3464] | 75 | def get_test_files(path): |
---|
[6218] | 76 | walk = os.walk(path) |
---|
[3464] | 77 | |
---|
| 78 | test_files = [] |
---|
| 79 | path_files = [] |
---|
| 80 | |
---|
[6218] | 81 | for (dirpath, dirnames, filenames) in walk: |
---|
| 82 | # exclude forbidden directories |
---|
| 83 | for e_dir in exclude_dirs: |
---|
| 84 | try: |
---|
| 85 | dirnames.remove(e_dir) |
---|
| 86 | except ValueError: |
---|
| 87 | pass |
---|
[6086] | 88 | |
---|
[6218] | 89 | # check for test_*.py files |
---|
| 90 | for filename in filenames: |
---|
| 91 | if filename.startswith('test_') and filename.endswith('.py'): |
---|
| 92 | test_files.append(filename) |
---|
| 93 | if dirpath not in path_files: |
---|
| 94 | path_files.append(dirpath) |
---|
| 95 | |
---|
[4804] | 96 | return test_files, path_files |
---|
[3464] | 97 | |
---|
| 98 | |
---|
[4414] | 99 | def regressionTest(test_verbose=False): |
---|
[6218] | 100 | # start off with where we are |
---|
[3464] | 101 | path = os.getcwd() |
---|
[6218] | 102 | print |
---|
| 103 | print 'Testing path: %s' % path |
---|
| 104 | |
---|
[6904] | 105 | # get the terminal width |
---|
| 106 | term_width = terminal_width() |
---|
| 107 | |
---|
[6218] | 108 | # explain what we are doing |
---|
| 109 | print |
---|
| 110 | print "The following directories will be skipped over:" |
---|
| 111 | exclude_dirs.sort() |
---|
[6904] | 112 | list_names(exclude_dirs, page_width=term_width) |
---|
[6218] | 113 | |
---|
| 114 | # get all test_*.py and enclosing directories |
---|
[3464] | 115 | test_files, path_files = get_test_files(path) |
---|
[7106] | 116 | path_files.sort() |
---|
[4804] | 117 | |
---|
[3464] | 118 | files = [x for x in test_files if not x == 'test_all.py'] |
---|
[6218] | 119 | files.sort() # Ensure same order on all platforms |
---|
[4804] | 120 | |
---|
[4461] | 121 | print |
---|
[6215] | 122 | print 'Paths searched:' |
---|
[6904] | 123 | list_names(path_files, os.path.basename, page_width=term_width) |
---|
[6218] | 124 | |
---|
[6215] | 125 | print |
---|
[6218] | 126 | print 'Files tested:' |
---|
[6904] | 127 | list_names(files, page_width=term_width) |
---|
[6220] | 128 | print |
---|
[6215] | 129 | |
---|
[6218] | 130 | # update system path with found paths |
---|
| 131 | for path in path_files: |
---|
| 132 | sys.path.append(path) |
---|
| 133 | |
---|
| 134 | # exclude files that we can't handle |
---|
| 135 | for file in exclude_files: |
---|
| 136 | print 'WARNING: File '+ file + ' to be excluded from testing' |
---|
| 137 | try: |
---|
| 138 | files.remove(file) |
---|
| 139 | except ValueError, e: |
---|
| 140 | msg = 'File "%s" was not found in test suite.\n' % file |
---|
| 141 | msg += 'Original error is "%s"\n' % e |
---|
| 142 | msg += 'Perhaps it should be removed from exclude list?' |
---|
| 143 | raise Exception, msg |
---|
[3464] | 144 | |
---|
[6218] | 145 | # import all test_*.py files |
---|
| 146 | # NOTE: This implies that test_*.py files MUST HAVE UNIQUE NAMES! |
---|
[3464] | 147 | filenameToModuleName = lambda f: os.path.splitext(f)[0] |
---|
| 148 | moduleNames = map(filenameToModuleName, files) |
---|
| 149 | modules = map(__import__, moduleNames) |
---|
[6086] | 150 | |
---|
[3464] | 151 | # Fix up the system path |
---|
| 152 | for file in path_files: |
---|
| 153 | sys.path.remove(file) |
---|
[6086] | 154 | |
---|
[6218] | 155 | # bundle up all the tests |
---|
[3464] | 156 | load = unittest.defaultTestLoader.loadTestsFromModule |
---|
[4414] | 157 | testCaseClasses = map(load, modules) |
---|
[4418] | 158 | |
---|
[4414] | 159 | if test_verbose is True: |
---|
[4415] | 160 | # Test the code by setting verbose to True. |
---|
| 161 | # The test cases have to be set up for this to work. |
---|
| 162 | # See test data manager for an example. |
---|
[4414] | 163 | for test_suite in testCaseClasses: |
---|
| 164 | for tests in test_suite._tests: |
---|
[4418] | 165 | # tests is of class TestSuite |
---|
| 166 | if len(tests._tests) > 1: |
---|
[4414] | 167 | # these are the test functions |
---|
| 168 | try: |
---|
[6086] | 169 | # Calls class method set_verbose in test case classes |
---|
[4414] | 170 | tests._tests[0].set_verbose() |
---|
| 171 | except: |
---|
[6086] | 172 | pass # No all classes have set_verbose |
---|
[6218] | 173 | |
---|
[4414] | 174 | return unittest.TestSuite(testCaseClasses) |
---|
[3464] | 175 | |
---|
[6086] | 176 | |
---|
[6218] | 177 | ## |
---|
| 178 | # @brief Check that the environment is sane. |
---|
| 179 | # @note Stops here if there is an error. |
---|
[4763] | 180 | def check_anuga_import(): |
---|
| 181 | try: |
---|
| 182 | # importing something that loads quickly |
---|
| 183 | import anuga.utilities.anuga_exceptions |
---|
| 184 | except ImportError: |
---|
| 185 | print "Python cannot import ANUGA module." |
---|
| 186 | print "Check you have followed all steps of its installation." |
---|
[6086] | 187 | import sys |
---|
| 188 | sys.exit() |
---|
[4763] | 189 | |
---|
[6086] | 190 | |
---|
[3464] | 191 | if __name__ == '__main__': |
---|
[4763] | 192 | check_anuga_import() |
---|
[6086] | 193 | |
---|
[4414] | 194 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
| 195 | test_verbose = True |
---|
[6086] | 196 | saveout = sys.stdout |
---|
[4414] | 197 | filename = ".temp" |
---|
| 198 | fid = open(filename, 'w') |
---|
| 199 | sys.stdout = fid |
---|
| 200 | else: |
---|
[6086] | 201 | test_verbose = False |
---|
[4414] | 202 | suite = regressionTest(test_verbose) |
---|
[3464] | 203 | runner = unittest.TextTestRunner() #verbosity=2 |
---|
| 204 | runner.run(suite) |
---|
[6086] | 205 | |
---|
[6220] | 206 | # timestamp at the end |
---|
| 207 | timestamp = time.asctime() |
---|
| 208 | version = aust.get_revision_number() |
---|
| 209 | print |
---|
| 210 | print 'Finished at %s, version %s' % (timestamp, version) |
---|
| 211 | |
---|
[4414] | 212 | # Cleaning up |
---|
| 213 | if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': |
---|
[6086] | 214 | sys.stdout = saveout |
---|
[4414] | 215 | #fid.close() # This was causing an error in windows |
---|
| 216 | #os.remove(filename) |
---|
| 217 | |
---|
[6417] | 218 | |
---|
| 219 | if sys.platform == 'win32': |
---|
[6718] | 220 | raw_input('Press the RETURN key') |
---|