pytest-dev / pytest-dev/pytest
`ImportPathMismatchError` from subprojects with same test folder structure
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
We have a big CI job that runs pytest against a couple (sub)projects checked out side by side. We added a __init__.py file in one of the tests folders to allow for some internal code reuse, and now the whole test suite fails to run with a ImportPathMismatchError.
Minimal reproduction example:
cd some_empty_root_dir
mkdir -p projectA/tests
touch projectA/tests/__init__.py
touch projectA/tests/conftest.py
mkdir -p projectB/tests
touch projectB/tests/__init__.py
touch projectB/tests/conftest.py
So file structure is as follows
$ tree
.
├── projectA
│ └── tests
│ ├── conftest.py
│ └── __init__.py
└── projectB
└── tests
├── conftest.py
└── __init__.py
Running pytest in each of the subprojects works fine (collected 0 items, but that is expected).
However, when running at the toplevel:
$ pytest
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.11.0, pytest-7.4.0, pluggy-1.2.0
rootdir: /tmp/fruplz
collected 0 items / 1 error
===================================================================================================== ERRORS ======================================================================================================
__________________________________________________________________________________________ ERROR collecting test session __________________________________________________________________________________________
.venv/lib/python3.11/site-packages/_pytest/config/__init__.py:642: in _importconftest
mod = import_path(conftestpath, mode=importmode, root=rootpath)
.venv/lib/python3.11/site-packages/_pytest/pathlib.py:588: in import_path
raise ImportPathMismatchError(module_name, module_file, path)
E _pytest.pathlib.ImportPathMismatchError: ('tests.conftest', '/tmp/fruplz/projectA/tests/conftest.py', PosixPath('/tmp/fruplz/projectB/tests/conftest.py'))
============================================================================================= short test summary info =============================================================================================
ERROR - _pytest.pathlib.ImportPathMismatchError: ('tests.conftest', '/tmp/fruplz/projectA/tests/conftest.py', PosixPath('/tmp/fruplz/projectB/tests/conftest.py'))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================ 1 error in 0.04s =================================================================================================
Apparently pytest doesn't like that both projectA/tests/conftest.py and projectB/tests/conftest.py map to tests.conftest.
Is there a workaround for this?
(using pytest 7.4.0)
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
Reproduce the failure with the projectA/projectB layout and top-level pytest invocation shown in the issue. Start by tracing the reported _pytest/config/init.py and _pytest/pathlib.py locations to understand how both conftest.py files resolve to tests.conftest. Done means providing a tested fix or documented workaround for side-by-side subprojects.
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