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