Skip to content

pytest 8.1.1: import file mismatch #219

Description

@detrout

Hello,

I was updating the Debian package for python-graphviz 0.20.2 and was having problems running with pytest 8.1.1.

___ ERROR collecting .pybuild/cpython3_3.11_graphviz/build/tests/conftest.py ___
import file mismatch:
imported module 'conftest' has this __file__ attribute:
  /<<PKGBUILDDIR>>/.pybuild/cpython3_3.11_graphviz/build/tests/backend/conftest.py
which is not the same as the test file we want to collect:
  /<<PKGBUILDDIR>>/.pybuild/cpython3_3.11_graphviz/build/tests/conftest.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

Debian's test process involves creating a build directory and then running the tests against that supported interpreter versions build directory, so paths are different than just running in the source directory.

As far as I can tell it looks like pytest is getting confused by the existence of two different conftest.py files in tests/ and tests/backends.

I solved the above issue for our build process by moving the contents of tests/backends/conftest.py into tests/conftest.py.

--- a/tests/backend/conftest.py
+++ /dev/null
@@ -1,22 +0,0 @@
-"""pytest fixtures for backend."""
-
-import pytest
-
-
-@pytest.fixture
-def mock_run(mocker):
-    yield mocker.patch('subprocess.run', autospec=True)
-
-
-@pytest.fixture
-def mock_popen(mocker):
-    yield mocker.patch('subprocess.Popen', autospec=True)
-
-
-@pytest.fixture
-def mock_startfile(mocker, platform):
-    if platform == 'windows':
-        kwargs = {'autospec': True}
-    else:
-        kwargs = {'create': True, 'new_callable': mocker.Mock}
-    yield mocker.patch('os.startfile', **kwargs)
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -98,3 +98,21 @@
 def unknown_platform(monkeypatch, name='nonplatform'):
     monkeypatch.setattr('graphviz.backend.viewing.PLATFORM', name)
     yield name
+
+@pytest.fixture
+def mock_run(mocker):
+    yield mocker.patch('subprocess.run', autospec=True)
+
+
+@pytest.fixture
+def mock_popen(mocker):
+    yield mocker.patch('subprocess.Popen', autospec=True)
+
+
+@pytest.fixture
+def mock_startfile(mocker, platform):
+    if platform == 'windows':
+        kwargs = {'autospec': True}
+    else:
+        kwargs = {'create': True, 'new_callable': mocker.Mock}
+    yield mocker.patch('os.startfile', **kwargs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions