comparison tests/test.py @ 5:a825f00fe062

add a different call method, rename the old one, and have a test for it
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 19 May 2011 12:26:29 -0700
parents
children ff272dcd5cd8
comparison
equal deleted inserted replaced
4:5ce55f6c8964 5:a825f00fe062
1 #!/usr/bin/env python
2
3 """
4 doctest runner
5 """
6
7 import doctest
8 import os
9 import sys
10 from optparse import OptionParser
11
12
13 def run_tests(raise_on_error=False, report_first=False):
14
15 # add results here
16 results = {}
17
18 # doctest arguments
19 directory = os.path.dirname(os.path.abspath(__file__))
20 extraglobs = {'here': directory}
21 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
22 if report_first:
23 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
24
25 # gather tests
26 tests = [ test for test in os.listdir(directory)
27 if test.endswith('.txt') ]
28
29 # run the tests
30 for test in tests:
31 try:
32 results[test] = doctest.testfile(test, **doctest_args)
33 except doctest.DocTestFailure, failure:
34 raise
35 except doctest.UnexpectedException, failure:
36 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
37
38 return results
39
40 def main(args=sys.argv[1:]):
41
42 # parse command line args
43 parser = OptionParser()
44 parser.add_option('--raise', dest='raise_on_error',
45 default=False, action='store_true',
46 help="raise on first error")
47 parser.add_option('--report-first', dest='report_first',
48 default=False, action='store_true',
49 help="report the first error only (all tests will still run)")
50 options, args = parser.parse_args(args)
51
52 # run the tests
53 results = run_tests(report_first=options.report_first,
54 raise_on_error=options.raise_on_error)
55 if sum([i.failed for i in results.values()]):
56 sys.exit(1) # error
57
58
59 if __name__ == '__main__':
60 main()