Changeset 7288
- Timestamp:
- Jul 3, 2009, 11:12:58 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/test_all.py
r7284 r7288 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 """ 1 from anuga.utilities.data_audit_wrapper import IP_verified 2 from tempfile import mktemp 7 3 8 # Author: Mark Pilgrim 9 # Modified by Ole Nielsen 4 import os 10 5 11 import unittest 12 import os 13 import sys 14 import tempfile 15 import time 16 import anuga.utilities.system_tools as aust 17 from anuga.utilities.terminal_width import terminal_width 6 buildroot = os.getcwd() 7 8 os.chdir('source') 9 os.chdir('anuga') 10 11 print 'Changing to', os.getcwd() #This is now different from buildroot 12 13 execfile('test_all.py') 18 14 19 15 20 #List files that should be excluded from the testing process. 21 #E.g. if they are known to fail and under development 22 exclude_files = [] 23 24 # Directories that should not be searched for test files. 25 exclude_dirs = ['pypar_dist', # Special requirements 26 '.svn', # subversion 27 'obsolete_code', # Never 28 'pymetis', # FIXME: ANU 29 'anuga_parallel', # FIXME: ANU 30 'pypar-numeric', # FIXME: REMOVE 31 'props', 'wcprops', 'prop-base', 'text-base', 'tmp'] 16 print 17 print '************************** NOTE *************************************' 18 print 'If all unit tests passed you should run the suite of validation tests' 19 print 'Go to the directory anuga_validation/automated_validation_tests' 20 print 'and run' 21 print ' python validate_all.py' 22 print 23 print 'These tests will take a few hours and will verify that ANUGA' 24 print 'produces the physical results expected.' 25 print '*********************************************************************' 32 26 33 27 34 ##35 # @brief List a string sequence on the screen in columns.36 # @param names Sequence of strings to list.37 # @param func Function to apply to each string in sequence.38 # @param col_width Force columns to this width (default calculated).39 # @param page_width Set displayable page width to this (default 132).40 def list_names(names, func=None, col_width=None, page_width=None):41 # set defaults42 p_width = page_width - 1 # set page width43 if p_width is None:44 p_width = 132 # default page width45 28 46 c_width = col_width # set column width 47 if c_width is None: 48 c_width = 0 49 for name in names: 50 if func: 51 name = func(name) 52 c_width = max(c_width, len(name)) 53 c_width += 2 # 2 column padding 54 55 # calculate number of columns allowed 56 max_columns = int(p_width / c_width) 57 58 # print columns 59 column = 0 60 for name in names: 61 if func: 62 name = func(name) 63 print '%-*s' % (c_width-1, name), 64 column += 1 65 if column >= max_columns: 66 column = 0 67 print 68 69 # if last line not finished, end it here 70 if column > 0: 71 print 29 # Temporary bail out 30 import sys; sys.exit() 72 31 73 32 74 ## 75 # @brief Get 'test_*.py' files and paths to directories. 76 # @param path Path to directory to start walk in. 77 # @return A tuple (<files>, <dirs>). 78 # @note Don't include any files in and below forbidden directories. 79 def get_test_files(path): 80 walk = os.walk(path) 33 #--------------------------- 34 # IP Data Audit (in source/anuga directory as well) 35 #--------------------------- 81 36 82 test_files = [] 83 path_files = [] 84 85 for (dirpath, dirnames, filenames) in walk: 86 # exclude forbidden directories 87 for e_dir in exclude_dirs: 88 try: 89 dirnames.remove(e_dir) 90 except ValueError: 91 pass 92 93 # check for test_*.py files 94 for filename in filenames: 95 if filename.startswith('test_') and filename.endswith('.py'): 96 test_files.append(filename) 97 if dirpath not in path_files: 98 path_files.append(dirpath) 99 100 return test_files, path_files 37 # Create temporary area for svn to export source files 38 # FIXME (Ole): It would be good to make sure these files 39 # are exactly the same as those walked over by the 40 # release script: create_distribution. 41 # 42 # Come to think of it - this is probably not the best 43 # place for this check. It may have to move up one level. 44 # What do you all think? 101 45 102 46 103 def regressionTest(test_verbose=False):104 # start off with where we are105 path = os.getcwd()106 print107 print 'Testing path: %s' % path108 47 109 # get the terminal width 110 term_width = terminal_width() 48 temp_dir = mktemp() 111 49 112 # explain what we are doing 113 print 114 print "The following directories will be skipped over:" 115 exclude_dirs.sort() 116 list_names(exclude_dirs, page_width=term_width) 50 print 'Temp dir', temp_dir 51 os.mkdir(temp_dir) 117 52 118 # get all test_*.py and enclosing directories 119 test_files, path_files = get_test_files(path) 120 path_files.sort() 53 # Get the ANUGA core source files 54 s = 'svn export . %s%sanuga' %(temp_dir, os.sep) 55 print s 56 os.system(s) 121 57 122 files = [x for x in test_files if not x == 'test_all.py'] 123 files.sort() # Ensure same order on all platforms 124 125 print 126 print 'Paths searched:' 127 list_names(path_files, os.path.basename, page_width=term_width) 128 129 print 130 print 'Files tested:' 131 list_names(files, page_width=term_width) 132 print 133 134 # update system path with found paths 135 for path in path_files: 136 sys.path.append(path) 137 138 # exclude files that we can't handle 139 for file in exclude_files: 140 print 'WARNING: File '+ file + ' to be excluded from testing' 141 try: 142 files.remove(file) 143 except ValueError, e: 144 msg = 'File "%s" was not found in test suite.\n' % file 145 msg += 'Original error is "%s"\n' % e 146 msg += 'Perhaps it should be removed from exclude list?' 147 raise Exception, msg 148 149 # import all test_*.py files 150 # NOTE: This implies that test_*.py files MUST HAVE UNIQUE NAMES! 151 filenameToModuleName = lambda f: os.path.splitext(f)[0] 152 moduleNames = map(filenameToModuleName, files) 153 modules = map(__import__, moduleNames) 154 155 # Fix up the system path 156 for file in path_files: 157 sys.path.remove(file) 158 159 # bundle up all the tests 160 load = unittest.defaultTestLoader.loadTestsFromModule 161 testCaseClasses = map(load, modules) 162 163 if test_verbose is True: 164 # Test the code by setting verbose to True. 165 # The test cases have to be set up for this to work. 166 # See test data manager for an example. 167 for test_suite in testCaseClasses: 168 for tests in test_suite._tests: 169 # tests is of class TestSuite 170 if len(tests._tests) > 1: 171 # these are the test functions 172 try: 173 # Calls class method set_verbose in test case classes 174 tests._tests[0].set_verbose() 175 except: 176 pass # No all classes have set_verbose 177 178 return unittest.TestSuite(testCaseClasses) 58 print 'Verifying data IP' 59 if not IP_verified(temp_dir): 60 msg = 'Files have not been verified for IP.\n' 61 msg += 'Each data file must have a license file with it.' 62 raise Exception, msg 179 63 180 64 181 ##182 # @brief Check that the environment is sane.183 # @note Stops here if there is an error.184 def check_anuga_import():185 try:186 # importing something that loads quickly187 import anuga.utilities.anuga_exceptions188 except ImportError:189 print "Python cannot import ANUGA module."190 print "Check you have followed all steps of its installation."191 import sys192 sys.exit()193 65 194 66 195 if __name__ == '__main__':196 check_anuga_import()197 198 if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V':199 test_verbose = True200 saveout = sys.stdout201 filename = ".temp"202 fid = open(filename, 'w')203 sys.stdout = fid204 else:205 test_verbose = False206 suite = regressionTest(test_verbose)207 runner = unittest.TextTestRunner() #verbosity=2208 runner.run(suite)209 210 # timestamp at the end211 timestamp = time.asctime()212 version = aust.get_revision_number()213 print214 print 'Finished at %s, version %s' % (timestamp, version)215 216 # Cleaning up217 if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V':218 sys.stdout = saveout219 #fid.close() # This was causing an error in windows220 #os.remove(filename)221 67 222 68 223 if sys.platform == 'win32':224 raw_input('Press the RETURN key')
Note: See TracChangeset
for help on using the changeset viewer.