annotate tests/test.py @ 24:13ed82d10144

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