我有几个包要为其编写 pytest
测试。所有的包都应该使用相同的 fixture 逻辑,所以我想让公共(public)测试逻辑( fixture )位于一些公共(public)路径中,而每个包测试都应该位于自己的路径中。
Repository 1:
=============
/path/to/PackageA/test_A.py
/path/to/PackageB/test_B.py
Repository 2:
=============
/different_path/to/Common/conftest.py
问题是当我在 test_A.py
或 test_B.py
上运行 pytest
时,pytest
没有' 找不到 conftest.py
中定义的固定装置。
尝试使用 --confcutdir
选项,但没有运气......
唯一对我有用的场景是从 /different_path/to/Common/
运行 pytest
,同时设置 pytest.ini
testpath =/path/to/PackageA/
(顺便说一句,运行 pytest/path/to/PackageA/
无效)。
最佳答案
该问题的答案是将 Common
转换为 pytest 插件。如果您已经为该 Common
项目创建了一个 setup.py
,只需将其添加到您的 setup
调用中即可:
# the following makes a plugin available to pytest
entry_points = {
'pytest11': [
'common = Common.plugin',
]
},
不过,您必须将 Common/conftest.py
重命名为 plugin.py
(或 conftest.py
以外的其他名称),因为 py.test 会特别对待该文件名。
如果您没有Common
的setup.py
,那么您可以通过添加addopts = - 让py.test 将其用作插件p Common.plugin
到 PackageA/pytest.ini
和 PackageB/pytest.ini
。
关于python - pytest - 从测试中分离 fixture 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379831/