pythonunit-testingpytestconftest

_pytest.pathlib.ImportPathMismatchError: 'tests.unit.conftest', when having different test directories and conftest


I am trying to use pytest to run tests in two different plugins as arranged in the following folder structure:

Folder Structure

src/some_package
└── plugin1
│   └── plugin1
│   └── tests
│   │   └──__init__.py
│   │   └── unit
│   │   │   └──__init__.py
│   │   │   └──conftest.py
│   │   │   └──test_plugin1.py
└── plugin2
│   └── plugin2
│   └── tests
│   │   └──__init__.py
│   │   └── unit
│   │   │   └──__init__.py
│   │   │   └──conftest.py
│   │   │   └──test_plugin2.py

I am configuring the options for the pytest run using the pyproject.toml since there are other settings as well.

pyproject.toml

[tool.pytest.ini_options]
markers = [
    "unittest: marks tests as unittest",
    "unittest-slow: marks tests as unittest that takes a long time",
]

When I try to run poetry run pytest -v -m unittest, I get

E   _pytest.pathlib.ImportPathMismatchError: ('tests.unit.conftest', 'src/some_package/plugin1/tests/unit/conftest.py', PosixPath('src/some_package/plugin2/tests/unit/conftest.py'))

Possible Solution 1.

If I add addopts = "--import-mode=importlib" to the pytest args in pyproject.toml, I get

E   ValueError: Plugin already registered under a different name: src/some_package/plugin1/tests/unit/conftest.py=<module 'tests.unit.conftest' from 'src/some_package/plugin2/tests/unit/conftest.py'>

Possible Solution 2.

Neither of the errors go away when using testpaths = ["src/some_package/plugin2/tests", "src/some_package/plugin1/tests"] or pythonpath = "src/some_package/plugin1:src/some_package/plugin2"

Note: It works if I run the tests individually poetry run pytest -v -m unittest src/some_package/plugin2/tests

Is there any solution for this issue so I can continue using pyproject.toml?


Solution

  • This can be avoided by renaming the tests folder in the plugin to tests_plugin1, tests_plugin2. This is particularly an issue in my case because I have each of the plugins path (src/some_package/plugin1) in the PYTHONPATH.