Skip to content

Commit

Permalink
fix(findcode): better finding of test code
Browse files Browse the repository at this point in the history
This fix sorts the --paths to look for code.
Longest paths come first. This is for cases where
one checks out other sources within his source directory.
In such a case this structure can happen:
my_package
my_package/src/some_other_package

Now when my_package gets found first. It logs an import
error and then goes on.
by sorting with longest paths first. I always get the most
specific match first. Since the loop is short circuited here,
it will never look at my_package.

This fixes #15
  • Loading branch information
do3cc committed Dec 17, 2014
1 parent 20c05ec commit 6ca9642
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,8 @@ zope.testrunner Changelog
4.4.4 (unreleased)
==================

- TBD
- When looking for the right location of test code, start with longest
location paths first. This fixes problems with nested code locations.


4.4.3 (2014-03-19)
Expand Down
7 changes: 6 additions & 1 deletion src/zope/testrunner/find.py
Expand Up @@ -171,8 +171,13 @@ def find_tests(options, found_suites=None):


def find_suites(options):
# Sort prefixes so that longest prefixes come first.
# That is because only first match is evaluated which
# can be a problem with nested source packages.
sorted_prefixes = [x for x in options.prefix]
sorted_prefixes.sort(key=lambda prefix: len(prefix[0]), reverse=True)
for fpath, package in find_test_files(options):
for (prefix, prefix_package) in options.prefix:
for (prefix, prefix_package) in sorted_prefixes:
if fpath.startswith(prefix) and package == prefix_package:
# strip prefix, strip .py suffix and convert separator to dots
noprefix = fpath[len(prefix):]
Expand Down
1 change: 1 addition & 0 deletions src/zope/testrunner/tests/test_doctest.py
Expand Up @@ -213,6 +213,7 @@ def test_suite():
'testrunner-progress.txt',
'testrunner-colors.txt',
'testrunner-simple.txt',
'testrunner-nestedcode.txt',
'testrunner-test-selection.txt',
'testrunner-verbose.txt',
'testrunner-repeat.txt',
Expand Down
23 changes: 23 additions & 0 deletions src/zope/testrunner/tests/testrunner-discovery.txt
Expand Up @@ -46,3 +46,26 @@ testrunner will exit with an error to prevent acidentally missing test cases:
sample1.sampletests_discover_notests
Total: 0 tests, 0 failures, 0 errors and 0 skipped in 0.000 seconds.
True

You can explicitly specify which tests to run by providing a function that
returns a unittest.TestSuite in the test modules (the name of the function can
be configured with the --suite-name parameter, it defaults to 'test_suite'). If
no such function is present, testrunner will use all classes in the module that
inherit from unittest.TestCase as tests:

>>> import os, sys
>>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')

>>> from zope import testrunner

>>> defaults = [
... ]
>>> sys.argv = ['test',
... ]
>>> testrunner.run(defaults)
Running zope.testrunner.layer.UnitTests tests:
Set up zope.testrunner.layer.UnitTests in N.NNN seconds.
Ran 2 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Tearing down left over layers:
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds.
False

0 comments on commit 6ca9642

Please sign in to comment.