Mercurial > hg > simpypi
annotate tests/test.py @ 64:bb8d993376aa
* convenience methods for multipart form
* include this in test globals
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 01 Mar 2012 18:15:34 -0800 |
parents | af1476a936fc |
children | 83327bc715be |
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 |
64
bb8d993376aa
* convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
12 import multipart |
6 | 13 import os |
8 | 14 import shutil |
6 | 15 import sys |
8 | 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 | 19 from optparse import OptionParser |
9 | 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""" |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 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
|
27 return python |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
28 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
|
29 |
6 | 30 def run_tests(raise_on_error=False, report_first=False): |
31 | |
32 # add results here | |
33 results = {} | |
34 | |
35 # doctest arguments | |
36 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
|
37 extraglobs = {'here': directory, |
2e60fb704454
add test server to the test globs
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
38 'create_virtualenv': create_virtualenv, |
64
bb8d993376aa
* convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
39 'testserver': testserver.TestWSGIServer, |
bb8d993376aa
* convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
40 'MultiPartForm': multipart.MultiPartForm |
bb8d993376aa
* convenience methods for multipart form
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
41 } |
6 | 42 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) |
43 if report_first: | |
44 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE | |
45 | |
46 # gather tests | |
63
af1476a936fc
add a multipart processor so we can post damn files
Jeff Hammel <jhammel@mozilla.com>
parents:
61
diff
changeset
|
47 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
|
48 if test.endswith('.txt')] |
6 | 49 |
50 # run the tests | |
51 for test in tests: | |
8 | 52 |
53 # make a temporary directory | |
54 tmpdir = tempfile.mkdtemp() | |
55 doctest_args['extraglobs']['directory'] = tmpdir | |
56 | |
6 | 57 try: |
58 results[test] = doctest.testfile(test, **doctest_args) | |
59 except doctest.DocTestFailure, failure: | |
60 raise | |
61 except doctest.UnexpectedException, failure: | |
62 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2] | |
8 | 63 finally: |
64 if os.path.exists(tmpdir): | |
65 shutil.rmtree(tmpdir) | |
6 | 66 |
67 return results | |
68 | |
69 def main(args=sys.argv[1:]): | |
70 | |
71 # parse command line args | |
72 parser = OptionParser(description=__doc__) | |
73 parser.add_option('--raise', dest='raise_on_error', | |
74 default=False, action='store_true', | |
75 help="raise on first error") | |
76 parser.add_option('--report-first', dest='report_first', | |
77 default=False, action='store_true', | |
78 help="report the first error only (all tests will still run)") | |
79 options, args = parser.parse_args(args) | |
80 | |
81 # run the tests | |
82 results = run_tests(**options.__dict__) | |
83 if sum([i.failed for i in results.values()]): | |
84 sys.exit(1) # error | |
85 | |
86 if __name__ == '__main__': | |
87 main() | |
88 |