pytest-dev / pytest-dev/pytest
Pytest Discovery broken when multiple modules have overlapping paths
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
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
mkdir -p pytest_discover_failure && cd pytest_discover_failure
mkdir -p a/tests/helpers
touch a/tests/__init__.py
touch a/tests/helpers/__init__.py
cat <<EOF > a/tests/helpers/test_helpers.py
def get_a():
return 'a'
EOF
cat <<EOF > a/tests/test_a.py
def test_a():
assert 1 == 1
EOF
cat <<EOF > a/conftest.py
import pytest
from tests.helpers.test_helpers import get_a
@pytest.fixture()
def some_fixture():
return get_a()
EOF
mkdir -p b/tests
touch b/tests/__init__.py
cat <<EOF > b/tests/conftest.py
import pytest
@pytest.fixture()
def some_fixture():
return 'b'
EOF
cat <<EOF > b/tests/test_b.py
def test_a():
assert 1 == 1
EOF
pytest --collect-only ./a ./b
Output:
$ 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at _getconftestmodules in src/_pytest/config/init.py, especially the conftest import path described in the issue. Reproduce the minimal example with pytest --collect-only ./a ./b, compare it with collecting ./a and ./b separately, and confirm that collection succeeds for both directories without the ModuleNotFoundError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100