annotate tests/test.py @ 32:486a4640fc31

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