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