annotate tests/test.py @ 72:b2ea14bf9f46

add a slash to the namespaced directory
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 02 Mar 2012 11:35:49 -0800
parents e62d2fddb275
children
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
58
cd88f80d1ab1 note about changing test fixtures
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
7 # XXX could use http://blog.ianbicking.org/2010/04/02/webtest-http-testing/
cd88f80d1ab1 note about changing test fixtures
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
8 # vs paste.fixture.TestApp:
cd88f80d1ab1 note about changing test fixtures
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
9 # http://pythonpaste.org/testing-applications.html
cd88f80d1ab1 note about changing test fixtures
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
10
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import doctest
64
bb8d993376aa * convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
12 import multipart
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import os
8
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
14 import shutil
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 import sys
8
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
16 import tempfile
56
2e60fb704454 add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
17 import testserver
55
143adebe4caa install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
18 import virtualenv
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 from optparse import OptionParser
9
9879140a2026 begin stubbing test app
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
20
55
143adebe4caa install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
21 def create_virtualenv(path):
143adebe4caa install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
22 """create a virtualenv and return the path to the python interpreter therein"""
68
e62d2fddb275 use the actual paths i mean
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
23
55
143adebe4caa install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
24 virtualenv.create_environment(path)
68
e62d2fddb275 use the actual paths i mean
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
25
65
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
26 for scripts, ext in (('bin', ''), ('Scripts', '.exe')):
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
27 scripts = os.path.join(path, scripts)
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
28 if os.path.exists(scripts):
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
29 break
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
30 else:
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
31 raise Exception("bin/Scripts not found in %s" % path)
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
32 binaries = ['python', 'easy_install', 'pip']
68
e62d2fddb275 use the actual paths i mean
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
33 binaries = dict([(i, os.path.join(scripts, i + ext)) for i in binaries])
65
83327bc715be make the virtualenv convenience method return more stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
34 return type('virtualenv', (), binaries)
55
143adebe4caa install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
35
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 def run_tests(raise_on_error=False, report_first=False):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 # add results here
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 results = {}
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 # doctest arguments
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 directory = os.path.dirname(os.path.abspath(__file__))
56
2e60fb704454 add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
43 extraglobs = {'here': directory,
2e60fb704454 add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
44 'create_virtualenv': create_virtualenv,
64
bb8d993376aa * convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
45 'testserver': testserver.TestWSGIServer,
bb8d993376aa * convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
46 'MultiPartForm': multipart.MultiPartForm
bb8d993376aa * convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
47 }
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 if report_first:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # gather tests
63
af1476a936fc add a multipart processor so we can post damn files
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
53 tests = [test for test in os.listdir(directory)
af1476a936fc add a multipart processor so we can post damn files
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
54 if test.endswith('.txt')]
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 # run the tests
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 for test in tests:
8
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
58
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
59 # make a temporary directory
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
60 tmpdir = tempfile.mkdtemp()
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
61 doctest_args['extraglobs']['directory'] = tmpdir
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
62
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 try:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 results[test] = doctest.testfile(test, **doctest_args)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 except doctest.DocTestFailure, failure:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 raise
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 except doctest.UnexpectedException, failure:
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 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
69 finally:
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
70 if os.path.exists(tmpdir):
15c7171941ea more stubbing for tests
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
71 shutil.rmtree(tmpdir)
6
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 return results
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 def main(args=sys.argv[1:]):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 # parse command line args
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 parser = OptionParser(description=__doc__)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 parser.add_option('--raise', dest='raise_on_error',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 default=False, action='store_true',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 help="raise on first error")
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 parser.add_option('--report-first', dest='report_first',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 default=False, action='store_true',
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 help="report the first error only (all tests will still run)")
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 options, args = parser.parse_args(args)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 # run the tests
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 results = run_tests(**options.__dict__)
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 if sum([i.failed for i in results.values()]):
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 sys.exit(1) # error
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 if __name__ == '__main__':
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 main()
232e67cac00c stub testing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94