The Wayback Machine - https://web.archive.org/web/20250601032545/https://github.com/python/cpython/issues/100290
Skip to content

unittest: TestLoader.discover() returns tests in package/__init__.py #100290

Open
@mherrmann

Description

@mherrmann

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.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions