0
|
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 def run_tests(raise_on_error=False, report_first=False):
|
|
13
|
|
14 # add results here
|
|
15 results = {}
|
|
16
|
|
17 # doctest arguments
|
|
18 directory = os.path.dirname(os.path.abspath(__file__))
|
|
19 extraglobs = {'here': directory}
|
|
20 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
|
|
21 if report_first:
|
|
22 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
|
|
23
|
|
24 # gather tests
|
|
25 tests = [ test for test in os.listdir(directory)
|
|
26 if test.endswith('.txt') ]
|
|
27
|
|
28 # run the tests
|
|
29 for test in tests:
|
|
30 try:
|
|
31 results[test] = doctest.testfile(test, **doctest_args)
|
|
32 except doctest.DocTestFailure, failure:
|
|
33 raise
|
|
34 except doctest.UnexpectedException, failure:
|
|
35 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
|
|
36
|
|
37 return results
|
|
38
|
|
39 def main(args=sys.argv[1:]):
|
|
40
|
|
41 # parse command line args
|
|
42 parser = OptionParser(description=__doc__)
|
|
43 parser.add_option('--raise', dest='raise_on_error',
|
|
44 default=False, action='store_true',
|
|
45 help="raise on first error")
|
|
46 parser.add_option('--report-first', dest='report_first',
|
|
47 default=False, action='store_true',
|
|
48 help="report the first error only (all tests will still run)")
|
|
49 options, args = parser.parse_args(args)
|
|
50
|
|
51 # run the tests
|
|
52 results = run_tests(**options.__dict__)
|
|
53 if sum([i.failed for i in results.values()]):
|
|
54 sys.exit(1) # error
|
|
55
|
|
56
|
|
57 if __name__ == '__main__':
|
|
58 main()
|
|
59
|