Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test - Error collecting when 2 conftest.py in different directories

Tags:

python

pytest

We using py.test. We try to put different conftest.py files in different folders to split our fixtures:

tests/api/
├── conftest.py
├── folder1
│   └── conftest.py
├── folder2
│   └── conftest.py

But when run the tests this error occurs:

____ ERROR collecting api/folder1/conftest.py ____
import file mismatch:
imported module 'conftest' has this __file__ attribute:
  /tests/api/folder2/conftest.py
which is not the same as the test file we want to collect:
  /tests/api/folder1/conftest.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

Why is that? How fix it?

PS. Removing __pycache__.pyc did not help.

PPS. __init__.py files already exist in each folder.

like image 294
Igor Lyubin Avatar asked Nov 19 '22 16:11

Igor Lyubin


1 Answers

I had the same issue. To solve this you need to create python packages instead of directories. Then pytest will look at the conftest.py in your package instead of root directory. Hope, this will help you. tests/api/ ├── conftest.py ├── package1 # not folder │ └── conftest.py ├── package2 # not folder │ └── conftest.py

like image 60
Oleksandr Makarenko Avatar answered Jan 12 '23 00:01

Oleksandr Makarenko