python - pytest - 从测试中分离 fixture 逻辑

标签 python testing pytest

我有几个包要为其编写 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.pytest_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 会特别对待该文件名。

如果您没有Commonsetup.py,那么您可以通过添加addopts = - 让py.test 将其用作插件p Common.pluginPackageA/pytest.iniPackageB/pytest.ini

关于python - pytest - 从测试中分离 fixture 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379831/

相关文章:

python - Python 中的散列

javascript - 基本的 JavaScript 内嵌函数解释

javascript - Protractor :您可以选择一个绑定(bind)而不将其包装在元素中吗?

python - 如何正确退出队列和 Qthread 以进行 pytest 测试?

python - 在 pytest 为 django 设置数据库之前安装 postgresql 扩展

python - 如何在 Python/Plotly 等值线图中放大 map ?

python - 从另一个线程或进程更新 Gtk.ProgressBar

testing - 我如何控制 Maven 中测试的执行顺序?

python - pytest 找不到位于测试目录下的辅助模块

python - 在 3.6 之前的 Python 版本中没有 __set_name__ 的解决方法