annotate tests/test.py @ 4:9fb08361ac8d

what the hell is wrong with me?
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 17 Dec 2012 11:08:45 -0800
parents 89e0c87f09ca
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 doctest runner
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import doctest
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 from optparse import OptionParser
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def run_tests(raise_on_error=False, report_first=False):
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 # add results here
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 results = {}
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 # doctest arguments
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 directory = os.path.dirname(os.path.abspath(__file__))
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 extraglobs = {'here': directory}
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if report_first:
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 # gather tests
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 tests = [ test for test in os.listdir(directory)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if test.endswith('.txt') ]
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 # run the tests
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 for test in tests:
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 try:
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 results[test] = doctest.testfile(test, **doctest_args)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 except doctest.DocTestFailure, failure:
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 raise
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 except doctest.UnexpectedException, failure:
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 return results
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 def main(args=sys.argv[1:]):
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 # parse command line args
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 parser = OptionParser(description=__doc__)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 parser.add_option('--raise', dest='raise_on_error',
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 default=False, action='store_true',
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 help="raise on first error")
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 parser.add_option('--report-first', dest='report_first',
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 default=False, action='store_true',
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 help="report the first error only (all tests will still run)")
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 options, args = parser.parse_args(args)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # run the tests
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 results = run_tests(**options.__dict__)
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 if sum([i.failed for i in results.values()]):
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 sys.exit(1) # error
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 if __name__ == '__main__':
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 main()
89e0c87f09ca initial (STUB) commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60