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