annotate tests/test.py @ 8:15c7171941ea

more stubbing for tests
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 13:50:28 -0800
parents 232e67cac00c
children 9879140a2026
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 """
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 doctest runner
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
8
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
13 from paste.fixture import TestApp
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 def run_tests(raise_on_error=False, report_first=False):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 # add results here
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 results = {}
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 # doctest arguments
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 directory = os.path.dirname(os.path.abspath(__file__))
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 extraglobs = {'here': directory}
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if report_first:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 # gather tests
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 tests = [ test for test in os.listdir(directory)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 if test.endswith('.txt') ]
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 # run the tests
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 for test in tests:
8
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
33
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
34 # make a temporary directory
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
35 tmpdir = tempfile.mkdtemp()
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
36 doctest_args['extraglobs']['directory'] = tmpdir
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
37
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 try:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 results[test] = doctest.testfile(test, **doctest_args)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 except doctest.DocTestFailure, failure:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 raise
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 except doctest.UnexpectedException, failure:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 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
44 finally:
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
45 if os.path.exists(tmpdir):
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
46 shutil.rmtree(tmpdir)
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 return results
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 def main(args=sys.argv[1:]):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # parse command line args
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 parser = OptionParser(description=__doc__)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 parser.add_option('--raise', dest='raise_on_error',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 default=False, action='store_true',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 help="raise on first error")
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 parser.add_option('--report-first', dest='report_first',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 default=False, action='store_true',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 help="report the first error only (all tests will still run)")
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 options, args = parser.parse_args(args)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 # run the tests
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 results = run_tests(**options.__dict__)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 if sum([i.failed for i in results.values()]):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 sys.exit(1) # error
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 if __name__ == '__main__':
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 main()
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70