source: anuga_core/source/anuga/test_all.py @ 4414

Last change on this file since 4414 was 4414, checked in by duncan, 18 years ago

checking in a set up of testing verbose sections of the code. May not be the final version.

File size: 5.8 KB
Line 
1"""Regression testing framework
2This module will search for scripts in the same directory named
3test_*.py.  Each such script should be a test suite that tests a
4module through PyUnit. This script will aggregate all
5found 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
11import unittest
12import os
13import sys
14import 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
20exclude_files = []
21if sys.platform != 'win32':  #Windows
22    exclude_files.append('test_advection.py') #Weave doesn't work on Linux
23
24exclude_dirs = ['pypar_dist', #Special requirements
25                'props', 'wcprops', 'prop-base', 'text-base', '.svn', #Svn
26                'tmp']
27
28
29print "The following directories will be skipped over;"
30for dir in exclude_dirs:
31    print dir
32print ""
33
34def 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 'Recursing into', 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
68def regressionTest(test_verbose=False):
69    path = os.getcwd()
70    test_files, path_files = get_test_files(path)
71    files = [x for x in test_files if not x == 'test_all.py']
72
73    print 'Testing path %s:' %('...'+path[-50:])
74    for file in files:
75        print '  ' + file
76    if globals().has_key('exclude_files'):
77        for file in exclude_files:
78            print 'WARNING: File '+ file + ' to be excluded from testing'
79        try:   
80            files.remove(file)
81        except ValueError, e:
82            msg = 'File "%s" was not found in test suite.\n' %file
83            msg += 'Original error is "%s"\n' %e
84            msg += 'Perhaps it should be removed from exclude list?' 
85            raise Exception, msg
86
87    filenameToModuleName = lambda f: os.path.splitext(f)[0]
88    moduleNames = map(filenameToModuleName, files)
89    modules = map(__import__, moduleNames)
90    # Fix up the system path
91    for file in path_files:
92        sys.path.remove(file)
93    load = unittest.defaultTestLoader.loadTestsFromModule
94    testCaseClasses = map(load, modules)
95    if test_verbose is True:
96        print "moduleNames", moduleNames
97        print "modules", modules
98        print "load", load
99        #print "weak", testCaseClasses.countTestCases()
100        #sys.exit()
101        i=0
102        from anuga.shallow_water.test_data_manager import Test_Data_Manager
103        from anuga.geospatial_data.test_geospatial_data import Test_Geospatial_data
104        #print "test_data_manager.Test_Data_Manager", type(Test_Data_Manager)
105        for test_suite in testCaseClasses:
106            i += 1
107            print "counting ", i
108            #testCaseClass.classVerbose = True
109            #testCaseClass.Verbose = True
110            #print "testCaseClass",testCaseClass
111            #print "testCaseClass",type(tests)
112            #print "weak", tests.countTestCases()
113            #print "weak", tests.__weakref__
114            #print "dic",  tests.__dict__
115            #print "testCaseClass.tests",  testCaseClass._tests[0]._tests[0].yah()
116            for tests in test_suite._tests:
117                #tests is of class TestSuite
118                print "tests weak", tests.__weakref__
119                if len(tests._tests) >1:
120                    # these are the test functions
121                    print "tests._tests[0]", tests._tests[0]
122                    print "tests._tests[0]", tests._tests[0].__dict__
123                    #print "tests._tests[0]", tests._tests[0].__name__
124                    try:
125                        # Calls set_verbose in the test case classes
126                        tests._tests[0].set_verbose()
127                    except:
128                        pass # No all classes have
129                    tests._tests[0].verbose=True # A call methods
130                    if type(tests._tests[0]) == type(Test_Data_Manager):
131                        print "testCaseClass is the class Test_Data_Manager"
132                        sys.exit()
133               
134                    if type(tests._tests[0]) == type(Test_Geospatial_data):
135                        print "testCaseClass is the class Test_Data_Manager"
136                        sys.exit()
137            if isinstance(tests, Test_Data_Manager):
138                print "testCaseClass is an instance of Test_Data_Manager"
139                sys.exit()
140            if type(tests) == type(Test_Data_Manager):
141                print "testCaseClass is the class Test_Data_Manager"
142                sys.exit()
143           
144        #sys.exit()   
145    return unittest.TestSuite(testCaseClasses)
146
147if __name__ == '__main__':
148    if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V':
149        test_verbose = True
150        saveout = sys.stdout   
151        filename = ".temp"
152        fid = open(filename, 'w')
153        sys.stdout = fid
154    else:
155        test_verbose = False       
156    suite = regressionTest(test_verbose)
157    runner = unittest.TextTestRunner() #verbosity=2
158    runner.run(suite)
159   
160    # Cleaning up
161    if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V':
162        sys.stdout = saveout
163        #fid.close() # This was causing an error in windows
164        #os.remove(filename)
165
Note: See TracBrowser for help on using the repository browser.