py.test - 在不同目录中收集 2 个 conftest.py 时出错

Igo*_*bin 5 python pytest

我们使用py.test。我们尝试将不同的conftest.py文件放在不同的文件夹中以拆分我们的灯具:

tests/api/
??? conftest.py
??? folder1
?   ??? conftest.py
??? folder2
?   ??? conftest.py
Run Code Online (Sandbox Code Playgroud)

但是当运行测试时会出现这个错误:

____ 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
Run Code Online (Sandbox Code Playgroud)

这是为什么?怎么修?

附注。删除__pycache__.pyc没有帮助。

聚苯乙烯。__init__.py每个文件夹中已经存在文件。

Fra*_*k T 0

您的用例听起来像pytest 文档中的这个示例。因此,我认为可以conftest.py在不同级别使用 s 来覆盖固定装置。

您看到的错误可能与不正确的导入有关。你的测试代码是直接从conftest文件导入的吗?您的conftest 文件是否从测试中导入?您的进口是相对的而不是绝对的?如果其中任何一条属实,那可能就是您的问题。我建议仅使用绝对导入,并避免在conftest.pys 和测试文件之间导入。