Skip to content

7.0.0 regression: Existence of conftest.py messes up package discovery with importlib + pythonpath mode (ModuleNotFoundError: No module named 'tests.<test-packages>') #9645

Closed
@bluenote10

Description

@bluenote10

Consider the following demo project:

setup.cfg

# The idea is to use `pythonpath = .` to enable imports from the `tests` folder
# like `import tests.<subpackages>`.
# Note that all involved directories have proper __init__.py, and importing e.g.
# `import tests.subpath.helper` works from a Python REPL.
[tool:pytest]
pythonpath = .
addopts =
    --import-mode importlib

tests/__init__.py

# just empty

tests/conftest.py (existence of this file breaks package discovery)

# just empty

tests/subpath/__init__.py

# just empty

tests/subpath/helper.py

# just empty

tests/subpath/test_something.py

import tests.subpath.helper


def test_something():
    assert True

pytest (version 7.0.0) errors with:

______________________________________________________________________________________________________________________________________________ ERROR collecting tests/subpath/test_something.py _______________________________________________________________________________________________________________________________________________
ImportError while importing test module '/tmp/demo_project/tests/subpath/test_something.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/subpath/test_something.py:1: in <module>
    import tests.subpath.helper
E   ModuleNotFoundError: No module named 'tests.subpath'; 'tests' is not a package
=========================================================================================================================================================== short test summary info ===========================================================================================================================================================
ERROR tests/subpath/test_something.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================================================================== 1 error in 0.08s ===============================================================================================================================================================

What's particularly surprising: The example works, when renaming the conftest.py to e.g. conftest2.py. I.e., it seems that the existence of the conftest.py implies that it is no longer possible to import from tests. Imports like this used to work with pre 7 pytest versions in importlib mode despite the existence of a conftest.py.

Metadata

Metadata

Assignees

Labels

type: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions