2
|
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(description=__doc__)
|
|
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(**options.__dict__)
|
|
54 if sum([i.failed for i in results.values()]):
|
|
55 sys.exit(1) # error
|
|
56
|
|
57
|
|
58 if __name__ == '__main__':
|
|
59 main()
|