generalize BlockGenerator to work with string keys
[pygr.git] / tests / runtest.py
blob4cc3db20cbe28752a652fb3a7e3e42b61070c7e7
1 #! /usr/bin/env python
2 """
3 Test runner for main pygr tests.
5 Collects all files ending in _test.py and executes them.
6 """
8 import os, sys, re, unittest, shutil, re, shutil
9 from testlib import testutil, testoptions
10 from testlib.unittest_extensions import PygrTestRunner
11 from pygr import logger
13 disable_threshold = 0 # global logging override
15 def all_tests():
16 "Returns all file names that end in _test.py"
17 patt = re.compile("_test.py$")
18 mods = os.listdir(os.path.normpath(os.path.dirname(__file__)))
19 mods = filter(patt.search, mods)
21 # some predictable order...
22 mods.sort()
23 return mods
25 def run(targets, options):
26 "Imports and runs the modules names that are contained in the 'targets'"
28 success = errors = skipped = 0
30 # run the tests by importing the module and getting its test suite
31 for name in targets:
32 try:
33 testutil.info('running tests for module %s' % name)
34 l = unittest.TestLoader()
35 suite = l.loadTestsFromName(name)
37 runner = PygrTestRunner(verbosity=options.verbosity,
38 descriptions=0)
40 logger.disable(disable_threshold) # set global override
41 results = runner.run(suite)
42 logger.disable(0) # clear global override
44 # count tests and errors
45 success += results.testsRun - \
46 len(results.errors) - \
47 len(results.failures) - \
48 len(results.skipped)
50 errors += len(results.errors) + len(results.failures)
51 skipped += len(results.skipped)
53 # if we're in strict mode stop on errors
54 if options.strict and errors:
55 testutil.error( "strict mode stops on errors" )
56 break
58 except ImportError:
59 testutil.error( "unable to import module '%s'" % name )
61 # summarize the run
62 testutil.info('=' * 59)
63 testutil.info('''\
64 %s tests passed, %s tests failed, %s tests skipped; %d total''' % \
65 (success, errors, skipped, success + errors + skipped))
67 return (success, errors, skipped)
69 if __name__ == '__main__':
70 # Make sure no messages are filtered out at first
71 logger.disable(0)
73 # gets the prebuild option parser
74 parser = testoptions.option_parser()
76 # parse the options
77 options, args = parser.parse_args()
79 # modules: from command line args or all modules
80 targets = args or all_tests()
82 # get rid of the .py ending in case full module names were passed in
83 # the command line
84 stripped_targets = []
85 for t in targets:
86 if t.endswith('.py'): t = t[:-3]
87 stripped_targets.append(t)
88 targets = stripped_targets
90 if options.port:
91 testutil.default_xmlrpc_port = options.port
93 # exclusion mode
94 if options.exclude:
95 targets = [ name for name in all_tests() if name not in targets ]
97 # disables debug messages at < 2 verbosity, debug+info at < 1
98 if options.verbosity < 1:
99 disable_threshold = 'INFO' # Should implicity disable DEBUG as well
100 elif options.verbosity < 2:
101 disable_threshold = 'DEBUG'
103 # cleans full entire test directory
104 if options.clean:
105 testutil.TEMPROOT.reset()
106 testutil.TEMPDIR = testutil.TEMPROOT.path # yikes!
108 # list patterns matching files to be removed here
109 patterns = [
110 "*.seqlen", "*.pureseq", "*.nin", "*.pin", "*.psd",
111 "*.psi", "*.psq", "*.psd", "*.nni", "*.nhr",
112 "*.nsi", "*.nsd", "*.nsq", "*.nnd",
114 testutil.remove_files(path=testutil.DATADIR, patterns=patterns)
116 # run all the tests
117 if options.coverage:
118 good, bad, skip = testutil.generate_coverage(run, 'coverage',
119 targets=targets,
120 options=options)
121 else:
122 good, bad, skip = run(targets=targets, options=options)
124 if bad:
125 sys.exit(-1)
127 sys.exit(0)