Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportPathMismatchError from subprojects with same test folder structure #11206

Open
soxofaan opened this issue Jul 13, 2023 · 1 comment
Open
Labels
topic: collection related to the collection phase

Comments

@soxofaan
Copy link
Contributor

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)

@giacomorebecchi
Copy link

I ran into this issue after updating VSCode, due to an experiment being run on vscode-python (see this issue) For the VSCode test plugin, the solution is simply to deactivate the experiment until they fix the bug.

For the CI/CD processes, instead, I run the pytest command on each subfolder:

pytest ./projectA/tests
pytest ./projectB/tests

By looking at the documentation, another option to avoid multiple commands seems:

pytest --import-mode importlib

However, this seems to break your intention of re-using some code in the tests subfolder.

My personal go-to, while suboptimal, would be to change the structure to:

$ tree
.
├── projectA
│   └── tests
│       └── test_projectA
│           ├── conftest.py
│           └── __init__.py
└── projectB
    └── tests
        └── test_projectB
            ├── conftest.py
            └── __init__.py

@soxofaan I hope this can be of any help!

@Zac-HD Zac-HD added the topic: collection related to the collection phase label Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase
Projects
None yet
Development

No branches or pull requests

3 participants