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

Last change on this file since 4796 was 4796, checked in by ole, 16 years ago

Disabled faulty test in Advection and also commented the logging info out. Ended up excluding it altogether. If anyone think we need advection, see ticket:205, and revive it.

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