Mercurial > hg > simpypi
annotate tests/test.py @ 58:cd88f80d1ab1
note about changing test fixtures
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 01 Mar 2012 15:37:32 -0800 |
parents | 6bfe6c59b64a |
children | 53ec5144f84f |
rev | line source |
---|---|
6 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
32 | 4 doctest runner for simpypi tests |
6 | 5 """ |
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 | 11 import doctest |
12 import os | |
8 | 13 import shutil |
6 | 14 import sys |
8 | 15 import tempfile |
56
2e60fb704454
add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
16 import testserver |
55
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
17 import virtualenv |
6 | 18 from optparse import OptionParser |
9 | 19 |
55
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
20 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
|
21 """create a virtualenv and return the path to the python interpreter therein""" |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
22 virtualenv.create_environment(path) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
23 for python in (('bin', 'python'), ('Scripts', 'python.exe')): |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
24 python = os.path.join(path, *python) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
25 if os.path.exists(python): |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
26 return python |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
27 raise Exception("Python binary not found in %s" % path) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
28 |
6 | 29 def run_tests(raise_on_error=False, report_first=False): |
30 | |
31 # add results here | |
32 results = {} | |
33 | |
34 # doctest arguments | |
35 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
|
36 extraglobs = {'here': directory, |
2e60fb704454
add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
37 'create_virtualenv': create_virtualenv, |
57
6bfe6c59b64a
add documentation and correct capitalization
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
38 'server': testserver.TestWSGIServer } |
6 | 39 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) |
40 if report_first: | |
41 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE | |
42 | |
43 # gather tests | |
44 tests = [ test for test in os.listdir(directory) | |
45 if test.endswith('.txt') ] | |
46 | |
47 # run the tests | |
48 for test in tests: | |
8 | 49 |
50 # make a temporary directory | |
51 tmpdir = tempfile.mkdtemp() | |
52 doctest_args['extraglobs']['directory'] = tmpdir | |
53 | |
6 | 54 try: |
55 results[test] = doctest.testfile(test, **doctest_args) | |
56 except doctest.DocTestFailure, failure: | |
57 raise | |
58 except doctest.UnexpectedException, failure: | |
59 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2] | |
8 | 60 finally: |
61 if os.path.exists(tmpdir): | |
62 shutil.rmtree(tmpdir) | |
6 | 63 |
64 return results | |
65 | |
66 def main(args=sys.argv[1:]): | |
67 | |
68 # parse command line args | |
69 parser = OptionParser(description=__doc__) | |
70 parser.add_option('--raise', dest='raise_on_error', | |
71 default=False, action='store_true', | |
72 help="raise on first error") | |
73 parser.add_option('--report-first', dest='report_first', | |
74 default=False, action='store_true', | |
75 help="report the first error only (all tests will still run)") | |
76 options, args = parser.parse_args(args) | |
77 | |
78 # run the tests | |
79 results = run_tests(**options.__dict__) | |
80 if sum([i.failed for i in results.values()]): | |
81 sys.exit(1) # error | |
82 | |
83 if __name__ == '__main__': | |
84 main() | |
85 |