Mercurial > hg > pyloader
annotate tests/test.py @ 98:d59f0425d705 default tip
version bump
| author | Jeff Hammel <k0scist@gmail.com> | 
|---|---|
| date | Mon, 27 May 2024 16:20:52 -0700 | 
| parents | 29ca60f428cc | 
| children | 
| rev | line source | 
|---|---|
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 1 #!/usr/bin/env python | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 2 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 3 """ | 
| 84 | 4 run tests for pyloader | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 5 """ | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 6 | 
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 7 # imports | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 8 import argparse | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 9 import doctest | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 10 import os | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 11 import sys | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 12 | 
| 84 | 13 directory = os.path.dirname(os.path.abspath(__file__)) | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 14 | 
| 84 | 15 def gather(): | 
| 16 """ gather tests""" | |
| 17 return [test for test in os.listdir(directory) | |
| 18 if test.endswith('.txt')] | |
| 19 | |
| 20 def run_tests(tests=None, raise_on_error=False, report_first=False): | |
| 21 """run the tests""" | |
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 22 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 23 # add results here | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 24 results = {} | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 25 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 26 # doctest arguments | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 27 extraglobs = {'here': directory} | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 28 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 29 if report_first: | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 30 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 31 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 32 # gather tests | 
| 84 | 33 tests = tests or gather() | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 34 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 35 # run the tests | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 36 for test in tests: | 
| 84 | 37 print ("Running: " + test) | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 38 try: | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 39 results[test] = doctest.testfile(test, **doctest_args) | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 40 except doctest.DocTestFailure, failure: | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 41 raise | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 42 except doctest.UnexpectedException, failure: | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 43 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2] | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 44 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 45 return results | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 46 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 47 def main(args=sys.argv[1:]): | 
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 48 """test runner CLI""" | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 49 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 50 # parse command line args | 
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 51 parser = argparse.ArgumentParser(description=__doc__) | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 52 parser.add_argument('tests', metavar='test', nargs="*", | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 53 help="tests to run; if omitted, run all") | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 54 parser.add_argument('--list', dest='list_tests', | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 55 action='store_true', default=False, | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 56 help="list tests and exit") | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 57 parser.add_argument('--raise', dest='raise_on_error', | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 58 default=False, action='store_true', | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 59 help="raise on first error") | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 60 parser.add_argument('--report-first', dest='report_first', | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 61 default=False, action='store_true', | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 62 help="report the first error only (all tests will still run)") | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 63 options = parser.parse_args(args) | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 64 | 
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 65 # gather tests | 
| 84 | 66 tests = gather() | 
| 67 | |
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 68 # filter tests | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 69 if options.tests: | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 70 missing = set(options.tests).difference(tests) | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 71 if missing: | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 72 parser.error("Test files not found: {0}; Should be in {1}".format(', '.join(missing), | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 73 ', '.join(tests))) | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 74 tests = options.tests | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 75 | 
| 84 | 76 if options.list_tests: | 
| 77 print ("\n".join(tests)) | |
| 78 sys.exit() | |
| 79 | |
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 80 # run the tests | 
| 87 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 81 results = run_tests(tests=tests, | 
| 
29ca60f428cc
STUB: tests/test.py tests/test_include.txt
 Jeff Hammel <k0scist@gmail.com> parents: 
84diff
changeset | 82 report_first=options.report_first, | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 83 raise_on_error=options.raise_on_error) | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 84 if sum([i.failed for i in results.values()]): | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 85 sys.exit(1) # error | 
| 84 | 86 | 
| 5 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 87 | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 88 if __name__ == '__main__': | 
| 
a825f00fe062
add a different call method, rename the old one, and have a test for it
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 89 main() | 
