pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.98k stars 2.66k forks source link

Pytest Discovery broken when multiple modules have overlapping paths #11275

Open rowillia opened 1 year ago

rowillia commented 1 year ago

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.

RonnyPfannschmidt commented 1 year ago

Currently you'd need init files in both a and b

The import roots proposal is supposed to resolve the issue

Zac-HD commented 1 year ago

@RonnyPfannschmidt the import roots proposal is https://github.com/pytest-dev/pytest/pull/10824, right?