| 1 | #! /usr/bin/env python | 
|---|
| 2 | """ | 
|---|
| 3 | Test runner for main pygr tests. | 
|---|
| 4 |  | 
|---|
| 5 | Collects all files ending in _test.py and executes them. | 
|---|
| 6 | """ | 
|---|
| 7 |  | 
|---|
| 8 | import os, sys, re, unittest, shutil, re, shutil | 
|---|
| 9 | from testlib import testutil, testoptions | 
|---|
| 10 | from testlib.unittest_extensions import TestRunner | 
|---|
| 11 | from genetrack import logger, conf | 
|---|
| 12 | import functional | 
|---|
| 13 |  | 
|---|
| 14 | def all_tests(): | 
|---|
| 15 |     "Returns all file names that end in _test.py" | 
|---|
| 16 |     patt = re.compile("_tests.py$") | 
|---|
| 17 |     mods = os.listdir(os.path.normpath(os.path.dirname(__file__))) | 
|---|
| 18 |     mods = filter(patt.search, mods) | 
|---|
| 19 |     mods = [ os.path.splitext(m)[0] for m in mods ] | 
|---|
| 20 |      | 
|---|
| 21 |     # some predictable order... | 
|---|
| 22 |     mods.sort()  | 
|---|
| 23 |     return mods | 
|---|
| 24 |  | 
|---|
| 25 | def run(targets, options): | 
|---|
| 26 |     "Imports and runs the modules names that are contained in the 'targets'" | 
|---|
| 27 |      | 
|---|
| 28 |     success = errors = skipped = 0 | 
|---|
| 29 |  | 
|---|
| 30 |     import_modules = [ 'doc_tests' ] | 
|---|
| 31 |     # run the tests by importing the module and getting its test suite | 
|---|
| 32 |     for name in targets: | 
|---|
| 33 |         try: | 
|---|
| 34 |  | 
|---|
| 35 |             if name in import_modules: | 
|---|
| 36 |                 mod = __import__(name) | 
|---|
| 37 |                 suite = mod.get_suite() | 
|---|
| 38 |             else: | 
|---|
| 39 |                 loader = unittest.TestLoader() | 
|---|
| 40 |                 suite = loader.loadTestsFromName(name) | 
|---|
| 41 |  | 
|---|
| 42 |             runner = TestRunner(verbosity=options.verbosity, | 
|---|
| 43 |                                     descriptions=0) | 
|---|
| 44 |              | 
|---|
| 45 |             results = runner.run(suite) | 
|---|
| 46 |              | 
|---|
| 47 |             # count tests and errors | 
|---|
| 48 |             success += results.testsRun - \ | 
|---|
| 49 |                        len(results.errors) - \ | 
|---|
| 50 |                        len(results.failures) - \ | 
|---|
| 51 |                        len(results.skipped) | 
|---|
| 52 |              | 
|---|
| 53 |             errors  += len(results.errors) + len(results.failures) | 
|---|
| 54 |             skipped += len(results.skipped) | 
|---|
| 55 |  | 
|---|
| 56 |             # if we're in strict mode stop on errors | 
|---|
| 57 |             if options.strict and errors: | 
|---|
| 58 |                 testutil.error( "strict mode stops on errors" ) | 
|---|
| 59 |                 break | 
|---|
| 60 |  | 
|---|
| 61 |         except ImportError: | 
|---|
| 62 |             testutil.error( "unable to import module '%s'" % name ) | 
|---|
| 63 |  | 
|---|
| 64 |     # enable the logger | 
|---|
| 65 |     logger.disable(None) | 
|---|
| 66 |  | 
|---|
| 67 |     # summarize the run | 
|---|
| 68 |     testutil.info('=' * 59) | 
|---|
| 69 |     testutil.info('''%s tests passed, %s tests failed, %s tests skipped; %d total''' % \ | 
|---|
| 70 |         (success, errors, skipped, success + errors + skipped)) | 
|---|
| 71 |  | 
|---|
| 72 |     return (success, errors, skipped) | 
|---|
| 73 |  | 
|---|
| 74 | if __name__ == '__main__': | 
|---|
| 75 |     # gets the prebuild option parser | 
|---|
| 76 |     parser = testoptions.option_parser() | 
|---|
| 77 |  | 
|---|
| 78 |     # parse the options | 
|---|
| 79 |     options, args = parser.parse_args() | 
|---|
| 80 |  | 
|---|
| 81 |     # modules: from command line args or all modules | 
|---|
| 82 |     targets = args or all_tests() | 
|---|
| 83 |  | 
|---|
| 84 |     # get rid of the .py ending in case full module names were  | 
|---|
| 85 |     # passed in the command line | 
|---|
| 86 |     targets = [ t.rstrip(".py") for t in targets ] | 
|---|
| 87 |  | 
|---|
| 88 |     # exclusion mode | 
|---|
| 89 |     if options.exclude: | 
|---|
| 90 |         targets = [ name for name in all_tests() if name not in targets ] | 
|---|
| 91 |  | 
|---|
| 92 |     if options.verbosity == 0: | 
|---|
| 93 |         logger.disable('INFO') | 
|---|
| 94 |     elif options.verbosity == 1: | 
|---|
| 95 |         logger.disable('DEBUG') | 
|---|
| 96 |     elif options.verbosity >= 2: | 
|---|
| 97 |         logger.disable(None) | 
|---|
| 98 |  | 
|---|
| 99 |     # cleans full entire test directory | 
|---|
| 100 |     if options.reset: | 
|---|
| 101 |         conf.reset_dir(conf.TEMP_DATA_DIR) | 
|---|
| 102 |      | 
|---|
| 103 |     # run all the tests | 
|---|
| 104 |     if options.coverage: | 
|---|
| 105 |         coverdir = conf.path_join(conf.TEST_DIR, 'coverage') | 
|---|
| 106 |         good, bad, skip = testutil.generate_coverage(run, coverdir, | 
|---|
| 107 |                                                      targets=targets, | 
|---|
| 108 |                                                      options=options) | 
|---|
| 109 |     else: | 
|---|
| 110 |         good, bad, skip = run(targets=targets, options=options) | 
|---|
| 111 |  | 
|---|
| 112 |     if bad: | 
|---|
| 113 |         sys.exit(-1) | 
|---|
| 114 |  | 
|---|
| 115 |     sys.exit(0) | 
|---|