[Zope3-checkins]
SVN: zope.testing/trunk/src/zope/testing/testrunner
Added --package-path option to deal with directories, containing
Jim Fulton
jim at zope.com
Fri Oct 28 16:01:54 EDT 2005
Log message for revision 39695:
Added --package-path option to deal with directories, containing
tests, that are knit into packages defined by code that isn't being
tested.
Also tried to improve the docs for --tests-pattern and
--test-file-pattern.
Changed:
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/__init__.py
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/__init__.py
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/__init__.py
A zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/sampletests.py
A zope.testing/trunk/src/zope/testing/testrunner-knit.txt
U zope.testing/trunk/src/zope/testing/testrunner.py
U zope.testing/trunk/src/zope/testing/testrunner.txt
-=-
Added: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/__init__.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/__init__.py 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/__init__.py 2005-10-28 20:01:54 UTC (rev 39695)
@@ -0,0 +1 @@
+#
Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/__init__.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/__init__.py 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/__init__.py 2005-10-28 20:01:54 UTC (rev 39695)
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Sample package that knits in extra directories.
+
+$Id$
+"""
+
+import os
+
+__path__.append(
+ os.path.join(
+ os.path.dirname( # testing
+ os.path.dirname( # testrunner-ex-knit-lib
+ os.path.dirname( # sample4
+ os.path.dirname(__file__) # products
+ )
+ )
+ )
+ , "testrunner-ex-pp-products"
+ )
+ )
+
Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-lib/sample4/products/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/__init__.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/__init__.py 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/__init__.py 2005-10-28 20:01:54 UTC (rev 39695)
@@ -0,0 +1 @@
+#
Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/sampletests.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/sampletests.py 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/sampletests.py 2005-10-28 20:01:54 UTC (rev 39695)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import unittest
+from zope.testing import doctest
+
+class Test(unittest.TestCase):
+
+ layer = 'samplelayers.Layer111'
+
+ def test_extra_test_in_products(self):
+ pass
+
+def test_suite():
+ return unittest.makeSuite(Test)
Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex-pp-products/sampletests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: zope.testing/trunk/src/zope/testing/testrunner-knit.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-knit.txt 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner-knit.txt 2005-10-28 20:01:54 UTC (rev 39695)
@@ -0,0 +1,70 @@
+Test Runner
+===========
+
+Knitting in extra package directories
+-------------------------------------
+
+Python packages have __path__ variables that can be manipulated to add
+extra directories cntaining software used in the packages. The
+testrunner needs to be given extra information about this sort of
+situation.
+
+Let's look at an example. The testrunner-ex-knit-lib directory
+is a directory that we want to add to the Python path, but that we
+don't want to search for tests. It has a sample4 package and a
+products subpackage. The products subpackage adds the
+testrunner-ex-knit-products to it's __path__. We want to run tests
+from the testrunner-ex-knit-products directory. When we import these
+tests, we need to import them from the sample4.products package. We
+can't use the --path option to name testrunner-ex-knit-products.
+It isn't enough to add the containing directory to the test path
+because then we wouldn't be able to determine the package name
+properly. We might be able to use the --package option to run the
+tests from the sample4/products package, but we want to run tests in
+testrunner-ex that aren't in this package.
+
+We can use the --package-path option in this case. The --package-path
+option is like the --test-path option in that it defines a path to be
+searched for tests without affecting the python path. It differs in
+that it supplied a package name that is added a profex when importing
+any modules found. The --package-path option takes *two* arguments, a
+package name and file path.
+
+ >>> import os.path, sys
+ >>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
+ >>> sys.path.append(os.path.join(this_directory, 'testrunner-ex-pp-lib'))
+ >>> defaults = [
+ ... '--path', directory_with_tests,
+ ... '--tests-pattern', '^sampletestsf?$',
+ ... '--package-path',
+ ... os.path.join(this_directory, 'testrunner-ex-pp-products'),
+ ... 'sample4.products',
+ ... ]
+
+ >>> from zope.testing import testrunner
+
+ >>> sys.argv = 'test --layer Layer111 -vv'.split()
+ >>> _ = testrunner.run(defaults)
+ Running tests at level 1
+ Running samplelayers.Layer111 tests:
+ Set up samplelayers.Layerx in 0.000 seconds.
+ Set up samplelayers.Layer1 in 0.000 seconds.
+ Set up samplelayers.Layer11 in 0.000 seconds.
+ Set up samplelayers.Layer111 in 0.000 seconds.
+ Running:
+ test_x1 (sample1.sampletests.test111.TestA)
+ test_y0 (sample1.sampletests.test111.TestA)
+ ...
+ test_y0 (sampletests.test111)
+ test_z1 (sampletests.test111)
+ testrunner-ex/sampletests/../sampletestsl.txt
+ test_extra_test_in_products (sample4.products.sampletests.Test)
+ Ran 35 tests with 0 failures and 0 errors in 0.008 seconds.
+ Tearing down left over layers:
+ Tear down samplelayers.Layer111 in 0.000 seconds.
+ Tear down samplelayers.Layerx in 0.000 seconds.
+ Tear down samplelayers.Layer11 in 0.000 seconds.
+ Tear down samplelayers.Layer1 in 0.000 seconds.
+
+In the example, the last test, test_extra_test_in_products, came from
+the products directory.
Property changes on: zope.testing/trunk/src/zope/testing/testrunner-knit.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: zope.testing/trunk/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.py 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner.py 2005-10-28 20:01:54 UTC (rev 39695)
@@ -860,7 +860,7 @@
def find_suites(options):
- for fpath in find_test_files(options):
+ for fpath, package in find_test_files(options):
for prefix in options.prefix:
if fpath.startswith(prefix):
# strip prefix, strip .py suffix and convert separator to dots
@@ -868,6 +868,9 @@
noext = strip_py_ext(options, noprefix)
assert noext is not None
module_name = noext.replace(os.path.sep, '.')
+ if package:
+ module_name = package + '.' + module_name
+
try:
module = import_name(module_name)
except:
@@ -917,13 +920,13 @@
def find_test_files(options):
found = {}
- for f in find_test_files_(options):
+ for f, package in find_test_files_(options):
if f in found:
continue
for filter in options.module:
if filter(f):
found[f] = 1
- yield f
+ yield f, package
break
identifier = re.compile(r'[_a-zA-Z]\w*$').match
@@ -949,7 +952,7 @@
else:
root2ext[key] = new
- for p in test_dirs(options, {}):
+ for (p, package) in test_dirs(options, {}):
for dirname, dirs, files in walk_with_symlinks(options, p):
if dirname != p and not contains_init_py(options, files):
continue # not a plausible test directory
@@ -971,7 +974,7 @@
winners = root2ext.values()
winners.sort()
for file in winners:
- yield file
+ yield file, package
def walk_with_symlinks(options, dir):
# TODO -- really should have test of this that uses symlinks
@@ -991,7 +994,7 @@
def remove_stale_bytecode(options):
if options.keepbytecode:
return
- for p in options.test_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:
@@ -1011,7 +1014,7 @@
for prefix in options.prefix:
if p.startswith(prefix):
seen[p] = 1
- yield p
+ yield p, ''
break
else:
for dpath in options.test_path:
@@ -1408,15 +1411,34 @@
will be run.
""")
+setup.add_option(
+ '--package-path', action="append", dest='package_path', nargs=2,
+ help="""\
+Specify a path to be searched for tests, but not added to the Python
+search path. Also specify a package for files found in this path.
+This is used to deal with directories that are stiched into packages
+that are not otherwise searched for tests.
+This option takes 2 arguments. The first is a path name. The second is
+the package name.
+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
-packages containing test modules or modules containing tests. When
-searching for tests, the test runner looks for modules or packages
-with this name.
+The test runner looks for modules containing tests. It uses this
+pattern to identify these modules. The modules may be either packages
+or python files.
+
+If a test module is a package, it uses the value given by the
+test-file-pattern to identify python files within the package
+containing tests.
""")
setup.add_option(
@@ -1429,9 +1451,8 @@
setup.add_option(
'--test-file-pattern', action="store", dest='test_file_pattern',
help="""\
-Specify the name of tests modules. Tests modules are packages
-containing test files or modules containing tests. When searching for
-tests, the test runner looks for modules or packages with this name.
+Specify a pattern for identifying python files within a tests package.
+See the documentation for the --tests-pattern option.
""")
setup.add_option(
@@ -1549,7 +1570,15 @@
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]
+
+ options.test_path = ([(path, '') for path in options.test_path]
+ +
+ [(os.path.abspath(path), package)
+ for (path, package) in options.package_path or ()
+ ])
+
+
+ options.prefix = [p + os.path.sep for (p, _) in options.test_path]
if options.all:
options.at_level = sys.maxint
@@ -1665,6 +1694,7 @@
'testrunner-wo-source.txt',
'testrunner-repeat.txt',
'testrunner-gc.txt',
+ 'testrunner-knit.txt',
setUp=setUp, tearDown=tearDown,
optionflags=doctest.ELLIPSIS+doctest.NORMALIZE_WHITESPACE,
checker=checker)
Modified: zope.testing/trunk/src/zope/testing/testrunner.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.txt 2005-10-28 18:33:12 UTC (rev 39694)
+++ zope.testing/trunk/src/zope/testing/testrunner.txt 2005-10-28 20:01:54 UTC (rev 39695)
@@ -84,4 +84,5 @@
- `Repeating Tests <testrunner-looping.txt>`_
- `Garbage Collection Control and Statistics <testrunner-gc.txt>`_
- `Debugging Memory Leaks <testrunner-leaks.txt>`_
+- `Knitting in extra package directories <testrunner-knit.txt>`_
- `Edge Cases <testrunner-edge-cases.txt>`_
More information about the Zope3-Checkins
mailing list