Skip to content

Commit 1eb074a

Browse files
committed
bpo-29642: Load tests from implicit packages.
Implicit PEP-420-style packages (i.e. missing __init__.py) were omitted, and this commit enables searching for tests in them.
1 parent 132ac38 commit 1eb074a

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

Doc/library/unittest.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,8 @@ Loading and running tests
16741674
the import failure is due to :exc:`SkipTest` being raised, it will be
16751675
recorded as a skip instead of an error.
16761676

1677-
If a package (a directory containing a file named :file:`__init__.py`) is
1677+
If a package (a directory containing a file named :file:`__init__.py` or
1678+
a :term:`namespace package <namespace package>`) is
16781679
found, the package will be checked for a ``load_tests`` function. If this
16791680
exists then it will be called
16801681
``package.load_tests(loader, tests, pattern)``. Test discovery takes care

Lib/unittest/loader.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
241241
If the start directory is not the top level directory then the top
242242
level directory must be specified separately.
243243
244-
If a test package name (directory with '__init__.py') matches the
244+
If a test package name (directory with or w/o '__init__.py') matches the
245245
pattern then the package will be checked for a 'load_tests' function. If
246246
this exists then it will be called with (loader, tests, pattern) unless
247247
the package has already had load_tests called from the same discovery
@@ -282,8 +282,6 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
282282
tests = []
283283
if os.path.isdir(os.path.abspath(start_dir)):
284284
start_dir = os.path.abspath(start_dir)
285-
if start_dir != top_level_dir:
286-
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
287285
else:
288286
# support for discovery from dotted module names
289287
try:
@@ -451,12 +449,6 @@ def _find_test_path(self, full_path, pattern, namespace=False):
451449
msg % (mod_name, module_dir, expected_dir))
452450
return self.loadTestsFromModule(module, pattern=pattern), False
453451
elif os.path.isdir(full_path):
454-
if (not namespace and
455-
not os.path.isfile(os.path.join(full_path, '__init__.py'))):
456-
return None, False
457-
458-
load_tests = None
459-
tests = None
460452
name = self._get_name_from_path(full_path)
461453
try:
462454
package = self._get_module_from_name(name)

Lib/unittest/test/test_discovery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def restore_isdir():
5454

5555
path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir',
5656
'test.foo', 'test-not-a-module.py', 'another_dir'],
57-
['test4.py', 'test3.py', ]]
57+
[], ['test4.py', 'test3.py']]
5858
os.listdir = lambda path: path_lists.pop(0)
5959
self.addCleanup(restore_listdir)
6060

@@ -65,7 +65,7 @@ def isdir(path):
6565

6666
def isfile(path):
6767
# another_dir is not a package and so shouldn't be recursed into
68-
return not path.endswith('dir') and not 'another_dir' in path
68+
return not path.endswith('dir') and 'another_dir' not in path
6969
os.path.isfile = isfile
7070
self.addCleanup(restore_isfile)
7171

@@ -85,7 +85,7 @@ def loadTestsFromModule(module, pattern=None):
8585
# The test suites found should be sorted alphabetically for reliable
8686
# execution order.
8787
expected = [[name + ' module tests'] for name in
88-
('test1', 'test2', 'test_dir')]
88+
('another_dir', 'test1', 'test2', 'test_dir')]
8989
expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
9090
('test3', 'test4')])
9191
self.assertEqual(suite, expected)

0 commit comments

Comments
 (0)