$ pytest --collect-only ./a ./b
ImportError while loading conftest '/Users/roy/code/oss/pytest_discover_failure/b/tests/conftest.py'.
ModuleNotFoundError: No module named 'tests.conftest'
Note that pytest --collect-only works for ./a and ./b individually.
Issue
Pytest Discovery appears to get confused if a module previously imported by a
conftest.py
module has a path that's a prefix of the module currently being imported. As the conftests are being imported in_getconftestmodules
(https://github.com/pytest-dev/pytest/blob/main/src/_pytest/config/__init__.py#L608-L616) we can get path conflicts.In our case it's causing a crash, in more pathalogical cases it could cause the wrong
conftest.py
file to be imported and referenced.pytest and operating system versions
pytest==7.4.0 macOS 13.5 & Ubuntu
Minimal Example
Output:
Note that
pytest --collect-only
works for./a
and./b
individually.