OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Ravin Kohli
  • Start date Start date
R

Ravin Kohli

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

Folder Structure

Code:
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

Code:
[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

Code:
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

Code:
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?
<p>I am trying to use pytest to run tests in two different plugins as arranged in the following folder structure:</p>
<p><strong>Folder Structure</strong></p>
<pre><code>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
</code></pre>
<p>I am configuring the options for the pytest run using the <code>pyproject.toml</code> since there are other settings as well.</p>
<p><strong>pyproject.toml</strong></p>
<pre><code>[tool.pytest.ini_options]
markers = [
"unittest: marks tests as unittest",
"unittest-slow: marks tests as unittest that takes a long time",
]

</code></pre>
<p>When I try to run <code>poetry run pytest -v -m unittest</code>, I get</p>
<pre><code>E _pytest.pathlib.ImportPathMismatchError: ('tests.unit.conftest', 'src/some_package/plugin1/tests/unit/conftest.py', PosixPath('src/some_package/plugin2/tests/unit/conftest.py'))
</code></pre>
<p><strong>Possible Solution 1.</strong></p>
<p>If I add <code>addopts = "--import-mode=importlib"</code> to the pytest args in <code>pyproject.toml</code>, I get</p>
<pre><code>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'>
</code></pre>
<p><strong>Possible Solution 2.</strong></p>
<p>Neither of the errors go away when using
<code>testpaths = ["src/some_package/plugin2/tests", "src/some_package/plugin1/tests"]</code> or <code>pythonpath = "src/some_package/plugin1:src/some_package/plugin2" </code></p>
<p><strong>Note</strong>: It works if I run the tests individually
<code>poetry run pytest -v -m unittest src/some_package/plugin2/tests</code></p>
<p>Is there any solution for this issue so I can continue using pyproject.toml?</p>
 

Latest posts

Top