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