Title: unittest does not discover tests in PEP420 packages
Type: behavior Stage: resolved
Components: Tests Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: adamchainz, methane, xtreak
Priority: normal Keywords:

Created on 2021-11-22 10:28 by adamchainz, last changed 2021-11-22 10:51 by adamchainz. This issue is now closed.

Messages (3)
msg406756 - (view) Author: Adam Johnson (adamchainz) * Date: 2021-11-22 10:28
unittest's test discovery does not descend into directories without ``. This avoids discovering test modules that are otherwise valid and importable, after PEP 420.

I've seen this more than once where there were valid looking test files not being discovered, and they bit rot. The tests had been run individually when created but never again.

(I created [flake8-no-pep420]( to avoid this problem on my projects.)

For example, take this directory structure:

$ tree
└── tests

1 directory, 1 file

$ cat tests/

It's valid to import the naughty file, which crashes:

$ python -c 'import tests.test_thing'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/.../tests/", line 1, in <module>
ZeroDivisionError: division by zero

But unittest does not discover it:

$ python -m unittest

Ran 0 tests in 0.000s


But, after creating an empty ``, the tests doth fail:

$ touch tests/

$ python -m unittest
ERROR: tests.test_thing (unittest.loader._FailedTest)
ImportError: Failed to import test module: tests.test_thing
Traceback (most recent call last):
  File "/.../unittest/", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/.../unittest/", line 377, in _get_module_from_name
  File "/.../tests/", line 1, in <module>
ZeroDivisionError: division by zero

Ran 1 test in 0.000s

FAILED (errors=1)
msg406759 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-11-22 10:46
This seems to be similar to
msg406760 - (view) Author: Adam Johnson (adamchainz) * Date: 2021-11-22 10:51
It's exactly that ticket. I missed that when searching for duplicates - I only searched for "pep420" and not "namespace packages". Mea culpa.
Date User Action Args
2021-11-22 10:51:25adamchainzsetstatus: open -> closed
stage: resolved
2021-11-22 10:51:21adamchainzsetresolution: duplicate
messages: + msg406760
2021-11-22 10:46:19xtreaksetnosy: + xtreak, methane
messages: + msg406759
2021-11-22 10:28:02adamchainzcreate