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

Last change on this file since 6126 was 6086, checked in by rwilson, 15 years ago

Changes to handle large files when Scientific.IO.NetCDF provides the feature.

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