comparison makeitso/python_package/tests/run_doctests.py @ 192:44bd0cc9bbc3

rename
author Jeff Hammel <k0scist@gmail.com>
date Thu, 08 May 2014 16:55:28 -0700
parents makeitso/python_package/tests/test_{{package}}.py@7f48c1e2f523
children
comparison
equal deleted inserted replaced
191:3cccaa4d48cc 192:44bd0cc9bbc3
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 doctest_args['optionsflags'] = doctest.ELLIPSIS
23 if report_first:
24 doctest_args['optionflags'] |= doctest.REPORT_ONLY_FIRST_FAILURE
25
26 # gather tests
27 tests = [test for test in os.listdir(directory)
28 if test.endswith('.txt')]
29
30 # run the tests
31 for test in tests:
32 try:
33 results[test] = doctest.testfile(test, **doctest_args)
34 except doctest.DocTestFailure, failure:
35 raise
36 except doctest.UnexpectedException, failure:
37 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
38
39 return results
40
41 def main(args=sys.argv[1:]):
42
43 # parse command line args
44 parser = OptionParser(description=__doc__)
45 parser.add_option('--raise', dest='raise_on_error',
46 default=False, action='store_true',
47 help="raise on first error")
48 parser.add_option('--report-first', dest='report_first',
49 default=False, action='store_true',
50 help="report the first error only (all tests will still run)")
51 options, args = parser.parse_args(args)
52
53 # run the tests
54 results = run_tests(**options.__dict__)
55 if sum([i.failed for i in results.values()]):
56 sys.exit(1) # error
57
58
59 if __name__ == '__main__':
60 main()