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
tests/conftest.py (existence of this file breaks package discovery)
tests/subpath/__init__.py
tests/subpath/helper.py
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.
Consider the following demo project:
setup.cfgtests/__init__.py# just emptytests/conftest.py(existence of this file breaks package discovery)# just emptytests/subpath/__init__.py# just emptytests/subpath/helper.py# just emptytests/subpath/test_something.pypytest (version 7.0.0) errors with:
What's particularly surprising: The example works, when renaming the
conftest.pyto e.g.conftest2.py. I.e., it seems that the existence of theconftest.pyimplies that it is no longer possible to import fromtests. Imports like this used to work with pre 7 pytest versions inimportlibmode despite the existence of aconftest.py.