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

pytest __init__.py collects another file in the directory #8976

Closed
toslunar opened this issue Aug 4, 2021 · 2 comments · Fixed by #11042
Closed

pytest __init__.py collects another file in the directory #8976

toslunar opened this issue Aug 4, 2021 · 2 comments · Fixed by #11042
Labels
topic: collection related to the collection phase

Comments

@toslunar
Copy link

toslunar commented Aug 4, 2021

I run pytest __init__.py by mistake, but it unexpectedly succeeded. pytest __init__.py seems collecting the tests in the first file in the directory instead. It's neither equivalent with pytest . nor "collected 0 items".

environment

pytest 6.2.4

python -c 'import sys; print(sys.version)'

3.9.6 (default, Jul 21 2021, 18:55:59)
[Clang 11.0.0 (clang-1100.0.33.16)]

pip list

Package    Version
---------- -------
attrs      21.2.0
iniconfig  1.1.1
packaging  21.0
pip        21.2.2
pluggy     0.13.1
py         1.10.0
pyparsing  2.4.7
pytest     6.2.4
setuptools 56.0.0
toml       0.10.2

minimal example

% COLUMNS=80 pytest -v foo_tests/__init__.py
============================= test session starts ==============================
platform darwin -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/tos/.pyenv/versions/3.9.6/envs/pytestnewissue/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/tos/Documents/tmp/20210804
collected 2 items

foo_tests/test_a.py::test_a1 PASSED                                      [ 50%]
foo_tests/test_a.py::test_a2 PASSED                                      [100%]

============================== 2 passed in 0.01s ===============================

with the following files.

foo_tests/__init__.py: empty file
foo_tests/test_a.py

def test_a1():
    pass

def test_a2():
    pass

foo_tests/test_b.py

def test_b1():
    pass

def test_b2():
    pass
@mdengler
Copy link

mdengler commented Jan 31, 2023

Still happening in 7.2.0:

$ mkdir foo_tests

$ touch foo_tests/__init__.py

$ cat > foo_tests/test_a.py
def test_a1():
    pass

def test_a2():
    pass

$ cat > foo_tests/test_b.py
def test_b1():
    pass

def test_b2():
    pass

$ COLUMNS=80 poetry run pytest --version
pytest 7.2.0

$ COLUMNS=80 poetry run pytest -v foo_tests/__init__.py
============================= test session starts ==============================
platform linux -- Python 3.9.16, pytest-7.2.0, pluggy-1.0.0 -- /home/martin/src/.../.venv/bin/python
cachedir: .pytest_cache
rootdir: /home/martin/src/..., configfile: pyproject.toml
plugins: anyio-3.6.2, xdist-tracker-0.0.2, cov-4.0.0, xdist-3.0.2, flakefinder-1.1.0, forked-1.4.0
collected 2 items                                                              

foo_tests/test_a.py::test_a1 PASSED                                      [ 50%]
foo_tests/test_a.py::test_a2 PASSED                                      [100%]

============================== 2 passed in 0.02s ===============================
$ 

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
@mdengler
Copy link

Excellent -- thanks!

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants