annotate tests/test.py @ 17:4566b3ac9838

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