Repository navigation
Testing an __init__.py file tests the entire package #9263
Description
Activity
- addedtopic: collectionrelated to the collection phaserelated to the collection phasetype: regressionindicates a problem that was introduced in a release which was working previouslyindicates a problem that was introduced in a release which was working previously
on Nov 3, 2021 I will try to make some time to look at this, but in the meantime it would be helpful for narrowing down the issue if you can check whether this is specific to doctests, or happens in general (with regular test functions).
I will try to make some time to look at this, but in the meantime it would be helpful for narrowing down the issue if you can check whether this is specific to doctests, or happens in general (with regular test functions).
OK, note I changed the name to
test_me_not.pyto be found by test discovery. The previous name ofdont_test_me.pywould not get picked up. Tests in__init__.pyalso do not get picked up.They seem to be consistent, but I'm not sure if expected.
% tree . └── package ├── __init__.py └── test_me_not.py % cat package/__init__.py def test_good(): assert round(22/7, 2) == 3.14 % cat package/test_me_not.py def test_bad(): assert 2 + 2 == 5
% pip install pytest==6.1.2 % pytest -q --tb=no package/__init__.py F [100%] =============================== short test summary info =============================== FAILED package/test_me_not.py::test_bad - assert (2 + 2) == 5 1 failed in 0.01s % pip install pytest==6.2.0 % pytest -q --tb=no package/__init__.py F [100%] =============================== short test summary info =============================== FAILED package/test_me_not.py::test_bad - assert (2 + 2) == 5 1 failed in 0.01s
Is this a duplicate of #8976?
Is this a duplicate of #8976?
Thank you for finding this. It doesn't seem to be exactly the same issue. The MRE in #8976 runs one
test_*file, but when adding--doctest-moduleslike in this PR it runs bothtest_*files.% tree . └── foo_tests ├── __init__.py ├── test_a.py └── test_b.py
% pytest --no-header foo_tests/__init__.py ======================== test session starts ========================= collected 2 items foo_tests/test_a.py .. [100%] ========================= 2 passed in 0.01s ==========================
% pytest --no-header --doctest-modules foo_tests/__init__.py ======================== test session starts ========================= collected 4 items foo_tests/test_a.py .. [ 50%] foo_tests/test_b.py .. [100%] ========================= 4 passed in 0.03s ==========================
- added a commit that references this issue
on May 23, 2022 Still exists in
7.2.0:$ tree ./package ./package ├── dont_test_me.py └── __init__.py 1 directory, 2 files $ cat package/__init__.py """ >>> round(22/7, 2) == 3.14 True """ $ cat package/dont_test_me.py """ >>> 2 + 2 == 5 True """ $ poetry run pytest --version pytest 7.2.0 $ poetry run pytest -q --tb=no --doctest-modules package/__init__.py .F [100%] ========================================================================================================== short test summary info =========================================================================================================== FAILED package/dont_test_me.py::package.dont_test_me 1 failed, 1 passed in 0.01s- added a commit that references this issue
on May 27, 2023 - added 2 commits that reference this issue
on Jun 3, 2023 - added 2 commits that reference this issue
on Jun 23, 2023 - added 2 commits that reference this issue
on Apr 18, 2026
I found a difference between 6.1.2 and 6.2.0. I would have expected the behavior of only testing
package/__init__.py, but in 6.2.0 it tests the entirety ofpackage/.I think it might be due to #8016 but I am not familiar with the internals of this library.