comparison makeitso/python_package/tests/test.py @ 123:8db34885ebe4

tame that beast called doctest
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 19 May 2011 10:56:16 -0700
parents ad5fd3eb6674
children 3eee157f4564
comparison
equal deleted inserted replaced
122:b2152efec89a 123:8db34885ebe4
3 """ 3 """
4 doctest runner 4 doctest runner
5 """ 5 """
6 6
7 import doctest 7 import doctest
8 import optparse
8 import os 9 import os
10 import sys
9 11
10 def run_tests(): 12 def run_tests(raise_on_error=False, report_first=False):
13
14 # add results here
15 results = {}
16
17 # doctest arguments
11 directory = os.path.dirname(os.path.abspath(__file__)) 18 directory = os.path.dirname(os.path.abspath(__file__))
12 extraglobs = {'here': directory} 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
13 tests = [ test for test in os.listdir(directory) 25 tests = [ test for test in os.listdir(directory)
14 if test.endswith('.txt') ] 26 if test.endswith('.txt') ]
27
28 # run the tests
15 for test in tests: 29 for test in tests:
16 doctest.testfile(test, extraglobs=extraglobs, raise_on_error=False) 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()
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(report_first=options.report_first,
52 raise_on_error=options.raise_on_error)
53 if sum([i.failed for i in results.values()]):
54 sys.exit(1) # error
55
17 56
18 if __name__ == '__main__': 57 if __name__ == '__main__':
19 run_tests() 58 main()