comparison tests/test.py @ 84:daf3a05a05fe

STUB: tests/test.py
author Jeff Hammel <k0scist@gmail.com>
date Fri, 21 Mar 2014 21:29:24 -0700
parents ff272dcd5cd8
children 29ca60f428cc
comparison
equal deleted inserted replaced
83:58eed691dca7 84:daf3a05a05fe
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 """ 3 """
4 doctest runner for pyloader 4 run tests for pyloader
5 """ 5 """
6 6
7 import doctest 7 import doctest
8 import os 8 import os
9 import sys 9 import sys
10 from optparse import OptionParser 10 from optparse import OptionParser
11 11
12 directory = os.path.dirname(os.path.abspath(__file__))
12 13
13 def run_tests(raise_on_error=False, report_first=False): 14 def gather():
15 """ gather tests"""
16 return [test for test in os.listdir(directory)
17 if test.endswith('.txt')]
18
19 def run_tests(tests=None, raise_on_error=False, report_first=False):
20 """run the tests"""
14 21
15 # add results here 22 # add results here
16 results = {} 23 results = {}
17 24
18 # doctest arguments 25 # doctest arguments
19 directory = os.path.dirname(os.path.abspath(__file__))
20 extraglobs = {'here': directory} 26 extraglobs = {'here': directory}
21 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) 27 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
22 if report_first: 28 if report_first:
23 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE 29 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
24 30
25 # gather tests 31 # gather tests
26 tests = [ test for test in os.listdir(directory) 32 tests = tests or gather()
27 if test.endswith('.txt') ]
28 33
29 # run the tests 34 # run the tests
30 for test in tests: 35 for test in tests:
36 print ("Running: " + test)
31 try: 37 try:
32 results[test] = doctest.testfile(test, **doctest_args) 38 results[test] = doctest.testfile(test, **doctest_args)
33 except doctest.DocTestFailure, failure: 39 except doctest.DocTestFailure, failure:
34 raise 40 raise
35 except doctest.UnexpectedException, failure: 41 except doctest.UnexpectedException, failure:
38 return results 44 return results
39 45
40 def main(args=sys.argv[1:]): 46 def main(args=sys.argv[1:]):
41 47
42 # parse command line args 48 # parse command line args
43 parser = OptionParser() 49 parser = OptionParser(description=__doc__)
50 parser.add_option('--list', dest='list_tests',
51 action='store_true', default=False,
52 help="list tests and exit")
44 parser.add_option('--raise', dest='raise_on_error', 53 parser.add_option('--raise', dest='raise_on_error',
45 default=False, action='store_true', 54 default=False, action='store_true',
46 help="raise on first error") 55 help="raise on first error")
47 parser.add_option('--report-first', dest='report_first', 56 parser.add_option('--report-first', dest='report_first',
48 default=False, action='store_true', 57 default=False, action='store_true',
49 help="report the first error only (all tests will still run)") 58 help="report the first error only (all tests will still run)")
50 options, args = parser.parse_args(args) 59 options, args = parser.parse_args(args)
51 60
61 # gathe tests
62 tests = gather()
63
64 if options.list_tests:
65 print ("\n".join(tests))
66 sys.exit()
67
52 # run the tests 68 # run the tests
53 results = run_tests(report_first=options.report_first, 69 results = run_tests(report_first=options.report_first,
54 raise_on_error=options.raise_on_error) 70 raise_on_error=options.raise_on_error)
55 if sum([i.failed for i in results.values()]): 71 if sum([i.failed for i in results.values()]):
56 sys.exit(1) # error 72 sys.exit(1) # error
57 73
58 74
59 if __name__ == '__main__': 75 if __name__ == '__main__':
60 main() 76 main()