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

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

Finished back-merge from Numeric trunk to numpy branch (and some code the other way).

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