annotate tests/test.py @ 13:c324ae5317ab

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