Description
According to the docs:
TestLoader.discover(start_dir, pattern='test*.py', top_level_dir=None)
Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them. Only test files that match pattern will be loaded.
However, tests are also loaded from __init__.py
. This does not match the pattern test*.py
.
To reproduce, create package/__init__.py
with the following contents:
import unittest
class Test(unittest.TestCase):
def test(self):
self.fail('')
Then run python3 -m unittest
. The expected output is Ran 0 tests
. But it actually is:
F
======================================================================
FAIL: test (package.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "package/__init__.py", line 5, in test
self.fail('')
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
So the test was discovered, despite being located in a file that does not match the pattern test*.py
.
The same happens when the TestCase
subclass is imported in any __init__.py
file.
I tested the above with Python 3.9.2. It does not happen for me with Python 2.7.18.