简体   繁体   中英

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

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.

Your use case sounds like this example in the pytest documentation . Because of that I think it's possible to use conftest.py s at different levels to override fixtures.

The errors you're seeing may be related to incorrect imports. Is your test code importing from conftest files directly? Are your conftest files importing from your tests? Are any of your imports relative instead of absolute ? If any of these are true, that may be your issue. I recommend only using absolute imports, and avoid imports between conftest.py s and test files.

Rename one (or both) of the test files Pytest is complaining about. Pytest is telling you in the error message to do this (ie change the basename, meaning don't name all your test files conftest.py ). For example, you can fix it by doing:

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

In your case, the module names conflict (you have three conftest.py s). This is a quirk of Pytest AFAIK. Pytest could get around this by managing full package/module paths: but it doesn't do this (probably for good reason, but I do not maintain/contribute to pytest so I can't shed light on the issue). Pytest is a fantastic framework (it's even telling you exactly why it can't run your tests): I'm sure they have a good reason for not supporting this behavior.

You claim that you want to:

separate tests and fixtures by different functionalities.

So do that. Separating the test fixtures/functionalities has nothing to do with what you name the files.

I commonly run into this error when splitting up unit/integration/acceptance tests. I split them up so I can run my (fast) unit tests without having to run my (potentially slow) integration/acceptance tests. I might have some module, call it Abc. And I have something like:

tests/
├── unit
│   └── test_abc.py
├── integration
│   └── test_abc.py

But then pytest barfs with the identical error you've shown, and so I just rename integration/test_abc.py to integration/test_abc_integration.py and move on with my day. Like this:

tests/
├── unit
│   └── test_abc.py
├── integration
│   └── test_abc_integration.py

Is it annoying? A little. How long does the fix take? 5 whole seconds.

PS You might have to remove __pycache__ directories or you .pyc files for the first run after you get the error you've posted about (if you don't you'll just get the same error again even if you rename).

PSS You can stop the Cpython interpreter (and most others) from writing out __pycache__ and .pyc files by calling python -B -m pytest ... . The -B option makes the interpreter not save the bytecode to your filesystem. This results in some performance penalty whenever you run your test suite, but the penalty is usually very small (milage may vary). I typically use this option because I don't like the clutter in my repositories and the performance loss is typically negligible.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM