1
Running Without Source Code
2
===========================
4
The ``--usecompiled`` option allows running tests in a tree without .py
5
source code, provided compiled .pyc or .pyo files exist (without
6
``--usecompiled``, .py files are necessary).
8
We have a very simple directory tree, under ``usecompiled/``, to test
9
this. Because we're going to delete its .py files, we want to work
12
>>> import os.path, shutil, sys, tempfile
13
>>> directory_with_tests = tempfile.mkdtemp()
15
>>> NEWNAME = "unlikely_package_name"
16
>>> src = os.path.join(this_directory, 'testrunner-ex', 'usecompiled')
17
>>> os.path.isdir(src)
19
>>> dst = os.path.join(directory_with_tests, NEWNAME)
20
>>> os.path.isdir(dst)
23
Have to use our own copying code, to avoid copying read-only SVN files that
24
can't be deleted later.
27
>>> for root, dirs, files in os.walk(src):
28
... dirs[:] = [d for d in dirs if d == "package"] # prune cruft
29
... os.mkdir(os.path.join(dst, root[n:]))
31
... shutil.copy(os.path.join(root, f),
32
... os.path.join(dst, root[n:], f))
34
Now run the tests in the copy:
36
>>> from zope.testing import testrunner
39
... '--path', directory_with_tests,
40
... '--tests-pattern', '^compiletest$',
41
... '--package', NEWNAME,
44
>>> sys.argv = ['test']
45
>>> testrunner.run(mydefaults)
46
Running tests at level 1
49
test1 (unlikely_package_name.compiletest.Test)
50
test2 (unlikely_package_name.compiletest.Test)
51
test1 (unlikely_package_name.package.compiletest.Test)
52
test2 (unlikely_package_name.package.compiletest.Test)
53
Ran 4 tests with 0 failures and 0 errors in N.NNN seconds.
56
If we delete the source files, it's normally a disaster: the test runner
57
doesn't believe any test files, or even packages, exist. Note that we pass
58
``--keepbytecode`` this time, because otherwise the test runner would
59
delete the compiled Python files too:
61
>>> for root, dirs, files in os.walk(dst):
63
... if f.endswith(".py"):
64
... os.remove(os.path.join(root, f))
65
>>> testrunner.run(mydefaults, ["test", "--keepbytecode"])
66
Running tests at level 1
67
Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
70
Finally, passing ``--usecompiled`` asks the test runner to treat .pyc
71
and .pyo files as adequate replacements for .py files. Note that the
72
output is the same as when running with .py source above. The absence
73
of "removing stale bytecode ..." messages shows that ``--usecompiled``
74
also implies ``--keepbytecode``:
76
>>> testrunner.run(mydefaults, ["test", "--usecompiled"])
77
Running tests at level 1
80
test1 (unlikely_package_name.compiletest.Test)
81
test2 (unlikely_package_name.compiletest.Test)
82
test1 (unlikely_package_name.package.compiletest.Test)
83
test2 (unlikely_package_name.package.compiletest.Test)
84
Ran 4 tests with 0 failures and 0 errors in N.NNN seconds.
87
Remove the temporary directory:
89
>>> shutil.rmtree(directory_with_tests)