annotate tests/test.py @ 12:1cdb25cef7ee

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