Mercurial > hg > pyloader
view tests/test.py @ 94:65ef62208d05
boldly moving forward
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 10:44:45 -0800 |
parents | 29ca60f428cc |
children |
line wrap: on
line source
#!/usr/bin/env python """ run tests for pyloader """ # imports import argparse import doctest import os import sys directory = os.path.dirname(os.path.abspath(__file__)) def gather(): """ gather tests""" return [test for test in os.listdir(directory) if test.endswith('.txt')] def run_tests(tests=None, raise_on_error=False, report_first=False): """run the tests""" # add results here results = {} # doctest arguments extraglobs = {'here': directory} doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) if report_first: doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE # gather tests tests = tests or gather() # run the tests for test in tests: print ("Running: " + test) try: results[test] = doctest.testfile(test, **doctest_args) except doctest.DocTestFailure, failure: raise except doctest.UnexpectedException, failure: raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2] return results def main(args=sys.argv[1:]): """test runner CLI""" # parse command line args parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('tests', metavar='test', nargs="*", help="tests to run; if omitted, run all") parser.add_argument('--list', dest='list_tests', action='store_true', default=False, help="list tests and exit") parser.add_argument('--raise', dest='raise_on_error', default=False, action='store_true', help="raise on first error") parser.add_argument('--report-first', dest='report_first', default=False, action='store_true', help="report the first error only (all tests will still run)") options = parser.parse_args(args) # gather tests tests = gather() # filter tests if options.tests: missing = set(options.tests).difference(tests) if missing: parser.error("Test files not found: {0}; Should be in {1}".format(', '.join(missing), ', '.join(tests))) tests = options.tests if options.list_tests: print ("\n".join(tests)) sys.exit() # run the tests results = run_tests(tests=tests, report_first=options.report_first, raise_on_error=options.raise_on_error) if sum([i.failed for i in results.values()]): sys.exit(1) # error if __name__ == '__main__': main()