pytest-dev / pytest-dev/pytest

Pytest Discovery broken when multiple modules have overlapping paths

Open
#11,275 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection topic: config
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.