Py.test - Collect error when 2 conftest.py in different directories

We are using py.test . We're trying to put different conftest.py files in different folders to break up our fixtures:

tests/api/
β”œβ”€β”€ conftest.py
β”œβ”€β”€ folder1
β”‚   └── conftest.py
β”œβ”€β”€ folder2
β”‚   └── conftest.py

      

But when running tests, this error comes up:

____ 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? How to fix it?

PS. Removing __pycache __ /. Pyc didn't help.

PPS. The __init__.py files already exist in every folder.

+3


source to share


1 answer


Your use case looks like this example in the pytest documentation . Because of this, I find it possible to use conftest.py

at different levels to override fixtures.



The errors you see may be related to incorrect imports. Is your test code importable directly from subclass files? Are your files to be imported from your tests? Are any of your imports relative and not absolute ? If so, this could be your problem. I recommend using only absolute imports and avoiding imports between conftest.py

and test files.

0


source







All Articles