[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/testrunner Added a --test-path option to specify a directory containing tests

Jim Fulton jim at zope.com
Sun Oct 9 12:34:27 EDT 2005


Log message for revision 39019:
  Added a --test-path option to specify a directory containing tests
  (top-level packages and modules) *without* specifying something to be
  added to the Python path.
  
  Fixed bugs in the setUp/tearDown code for the testrunner's tests.
  In particular, we weren't checkpointing the path and argument data
  properly and we weren't checkpointing module state at all.
  

Changed:
  U   zope.testing/trunk/src/zope/testing/testrunner-edge-cases.txt
  U   zope.testing/trunk/src/zope/testing/testrunner.py

-=-
Modified: zope.testing/trunk/src/zope/testing/testrunner-edge-cases.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-edge-cases.txt	2005-10-09 15:17:23 UTC (rev 39018)
+++ zope.testing/trunk/src/zope/testing/testrunner-edge-cases.txt	2005-10-09 16:34:26 UTC (rev 39019)
@@ -4,17 +4,44 @@
 This document has some edge-case examples to test various aspects of
 the test runner.
 
+Separating Python path and test directories
+-------------------------------------------
+
+The --path option defines a directory to be searched for tests *and* a
+directory to be added to Python's search path.  The --test-path option
+can be used when you want to set a test search path without also
+affecting the Python path:
+
     >>> import os, sys
     >>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
-    >>> sys.path.append(directory_with_tests)
 
     >>> from zope.testing import testrunner
 
     >>> defaults = [
-    ...     '--path', directory_with_tests,
+    ...     '--test-path', directory_with_tests,
     ...     '--tests-pattern', '^sampletestsf?$',
     ...     ]
+    >>> sys.argv = ['test']
+    >>> testrunner.run(defaults)
+    ... # doctest: +ELLIPSIS
+    Test-module import failures:
+    <BLANKLINE>
+    Module: sampletestsf
+    <BLANKLINE>
+    ImportError: No module named sampletestsf
+    ...
 
+    >>> sys.path.append(directory_with_tests)
+    >>> sys.argv = ['test']
+    >>> testrunner.run(defaults)
+    ... # doctest: +ELLIPSIS
+    Running unit tests:
+      Ran 192 tests with 0 failures and 0 errors in 0.028 seconds.
+    Running samplelayers.Layer1 tests:
+      Set up samplelayers.Layer1 in 0.000 seconds.
+      Ran 9 tests with 0 failures and 0 errors in 0.000 seconds.
+    ...
+
 Debugging
 ---------
 

Modified: zope.testing/trunk/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.py	2005-10-09 15:17:23 UTC (rev 39018)
+++ zope.testing/trunk/src/zope/testing/testrunner.py	2005-10-09 16:34:26 UTC (rev 39019)
@@ -873,7 +873,7 @@
 def remove_stale_bytecode(options):
     if options.keepbytecode:
         return
-    for p in options.path:
+    for p in options.test_path:
         for dirname, dirs, files in walk_with_symlinks(options, p):
             for file in files:
                 if file[-4:] in compiled_sufixes and file[:-1] not in files:
@@ -896,7 +896,7 @@
                         yield p
                         break
     else:
-        for dpath in options.path:
+        for dpath in options.test_path:
             yield dpath
 
 
@@ -1171,9 +1171,25 @@
 usually specified by the test-runner script itself, rather than by
 users of the script, although it can be overridden by users.  Only
 tests found in the path will be run.
+
+This option also specifies directories to be searched for tests.
+See the search_directory.
 """)
 
 setup.add_option(
+    '--test-path', action="append", dest='test_path',
+    help="""\
+Specify a path to be searched for tests, but not added to the Python
+search path.  This option can be used multiple times to specify
+multiple search paths.  The path is usually specified by the
+test-runner script itself, rather than by users of the script,
+although it can be overridden by users.  Only tests found in the path
+will be run.
+""")
+
+
+
+setup.add_option(
     '--tests-pattern', action="store", dest='tests_pattern',
     help="""\
 Specify the pattern for identifying tests modules. Tests modules are
@@ -1307,8 +1323,10 @@
     if options.package:
         options.package = [p.replace('/', '.').replace('\\', '.')
                            for p in options.package]
-    options.path = map(os.path.abspath, options.path)
-    options.prefix = [p + os.path.sep for p in options.path]
+    options.path = map(os.path.abspath, options.path or ())
+    options.test_path = map(os.path.abspath, options.test_path or ())
+    options.test_path += options.path
+    options.prefix = [p + os.path.sep for p in options.test_path]
     if options.all:
         options.at_level = sys.maxint
 
@@ -1371,12 +1389,18 @@
         ])
 
     def setUp(test):
-        test.globs['saved-sys-info'] = sys.path, sys.argv
+        test.globs['saved-sys-info'] = (
+            sys.path[:],
+            sys.argv[:],
+            sys.modules.copy()
+            )
         test.globs['this_directory'] = os.path.split(__file__)[0]
         test.globs['testrunner_script'] = __file__
 
     def tearDown(test):
-        sys.path, sys.argv = test.globs['saved-sys-info']
+        sys.path[:], sys.argv[:] = test.globs['saved-sys-info'][:2]
+        sys.modules.clear()
+        sys.modules.update(test.globs['saved-sys-info'][2])
 
     suite = doctest.DocFileSuite(
         'testrunner-arguments.txt',



More information about the Zope3-Checkins mailing list