source: branches/numpy/anuga/test_all.py @ 6304

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

Initial commit of numpy changes. Still a long way to go.

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