annotate tests/test.py @ 87:29ca60f428cc

STUB: tests/test.py tests/test_include.txt
author Jeff Hammel <k0scist@gmail.com>
date Fri, 21 Mar 2014 22:27:51 -0700
parents daf3a05a05fe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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: 84
diff changeset
7 # imports
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
15 def gather():
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
16 """ gather tests"""
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
17 return [test for test in os.listdir(directory)
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
18 if test.endswith('.txt')]
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
19
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
20 def run_tests(tests=None, raise_on_error=False, report_first=False):
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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: 84
diff 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: 84
diff changeset
51 parser = argparse.ArgumentParser(description=__doc__)
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
52 parser.add_argument('tests', metavar='test', nargs="*",
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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: 84
diff changeset
54 parser.add_argument('--list', dest='list_tests',
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
55 action='store_true', default=False,
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
56 help="list tests and exit")
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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: 84
diff changeset
58 default=False, action='store_true',
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
59 help="raise on first error")
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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: 84
diff changeset
61 default=False, action='store_true',
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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: 84
diff 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: 84
diff changeset
65 # gather tests
84
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
66 tests = gather()
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
67
87
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
68 # filter tests
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
69 if options.tests:
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
70 missing = set(options.tests).difference(tests)
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
71 if missing:
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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: 84
diff changeset
73 ', '.join(tests)))
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
74 tests = options.tests
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff changeset
75
84
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
76 if options.list_tests:
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
77 print ("\n".join(tests))
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
78 sys.exit()
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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: 84
diff changeset
81 results = run_tests(tests=tests,
29ca60f428cc STUB: tests/test.py tests/test_include.txt
Jeff Hammel <k0scist@gmail.com>
parents: 84
diff 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
daf3a05a05fe STUB: tests/test.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
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()