Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing an __init__.py file tests the entire package #9263

Closed
spagh-eddie opened this issue Nov 3, 2021 · 5 comments · Fixed by #11042
Closed

Testing an __init__.py file tests the entire package #9263

spagh-eddie opened this issue Nov 3, 2021 · 5 comments · Fixed by #11042
Labels
topic: collection related to the collection phase type: regression indicates a problem that was introduced in a release which was working previously

Comments

@spagh-eddie
Copy link
Contributor

spagh-eddie commented Nov 3, 2021

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 of package/.

I think it might be due to #8016 but I am not familiar with the internals of this library.

% tree                     
.
└── package
    ├── __init__.py
    └── dont_test_me.py

% cat package/__init__.py 
"""
>>> round(22/7, 2) == 3.14
True
"""

% cat package/dont_test_me.py 
"""
>>> 2 + 2 == 5
True
"""
% pip install pytest==6.1.2  
% pytest -q --tb=no --doctest-modules package/__init__.py
.                                                                                  [100%]
1 passed in 0.02s

% pip install pytest==6.2.0  
% 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.02s
@Zac-HD Zac-HD added topic: collection related to the collection phase type: regression indicates a problem that was introduced in a release which was working previously labels Nov 3, 2021
@bluetech
Copy link
Member

bluetech commented 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).

@spagh-eddie
Copy link
Contributor Author

spagh-eddie commented Nov 4, 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).

OK, note I changed the name to test_me_not.py to be found by test discovery. The previous name of dont_test_me.py would not get picked up. Tests in __init__.py also 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

@The-Compiler
Copy link
Member

Is this a duplicate of #8976?

@spagh-eddie
Copy link
Contributor Author

spagh-eddie commented Nov 26, 2021

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-modules like in this PR it runs both test_* 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 ==========================

drothlis added a commit to stb-tester/stb-tester that referenced this issue May 23, 2022
It causes pytest to run some of the tests twice (and they fail the
second time because they have exhausted a generator passed as a test
fixture). This happens with pytest 6.2.5 (Ubuntu 22.04) but not pytest
4.6.9 (Ubuntu 20.04). It looks like
<pytest-dev/pytest#9263>.
@mdengler
Copy link

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

bluetech added a commit to bluetech/pytest that referenced this issue May 27, 2023
….py` Module

Previously it would collect the entire package, but this is not what
users expect.

Refs pytest-dev#3749
Fixes pytest-dev#8976
Fixes pytest-dev#9263
Fixes pytest-dev#9313
bluetech added a commit to bluetech/pytest that referenced this issue Jun 3, 2023
….py` Module

Previously it would collect the entire package, but this is not what
users expect.

Refs pytest-dev#3749
Fixes pytest-dev#8976
Fixes pytest-dev#9263
Fixes pytest-dev#9313
bluetech added a commit to bluetech/pytest that referenced this issue Jun 3, 2023
….py` Module

Previously it would collect the entire package, but this is not what
users expect.

Refs pytest-dev#3749
Fixes pytest-dev#8976
Fixes pytest-dev#9263
Fixes pytest-dev#9313
bluetech added a commit to bluetech/pytest that referenced this issue Jun 23, 2023
….py` Module

Previously it would collect the entire package, but this is not what
users expect.

Refs pytest-dev#3749
Fixes pytest-dev#8976
Fixes pytest-dev#9263
Fixes pytest-dev#9313
bluetech added a commit to bluetech/pytest that referenced this issue Jun 23, 2023
….py` Module

Previously it would collect the entire package, but this is not what
users expect.

Refs pytest-dev#3749
Fixes pytest-dev#8976
Fixes pytest-dev#9263
Fixes pytest-dev#9313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants