Skip to content

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

@toslunar

Description

@toslunar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions