python - 单独目录中的 pytest fixtures

标签 python testing pytest fixtures

我正在寻求创建一个 pytest 结构,在其中我可以将固定装置与测试完全分开。这种分离的原因是我想将 fixtures 目录作为外部项包含在 subversion 中并在多个项目之间共享。

所需结构的树

project
|   conftest.py
|
+---fixtures
|       __init__.py
|       conftest.py
|       fixture_cifs.py
|       fixture_ftp.py
|       fixture_service.py
|
\---tests
    |   test_sometest1.py
    |   test_sometest2.py
    |
    \---configurations
            sometest1.conf
            sometest2.conf

我想在单独的文件中实现每个装置的功能,以避免单个巨大的 conftest.pyconftest.py 将只包含包装器以返回每个用 @pytest.fixture 注释的 fixture 的实例。当conftest.pyfixture_*.pytest_*.py文件都在时,fixture和test一起使用是没有问题的在同一目录中。

但是,当固定装置在子目录中分开时,我从 pytest fixture 'cifs' not found, available fixtures: ... 收到错误。我还没有找到任何文档来解释如何在 test_*.pytest_*.py 附近的 conftest.py 之外放置固定装置, 但没有任何迹象表明这也不行。

如何在使用 pytest 时将 fixture 放在它们自己的子目录中?

最佳答案

请在您的 conftest.py

中添加以下内容
  import pytest

  pytest_plugins = [
   "fixtures.conftest",
   "fixtures.fixture_cifs",
   "fixtures.fixture_ftp",
   "fixtures.fixture_service"
  ]

这确保 fixtures/ 下声明的所有固定装置都将被 pytest

找到

As a note that the respective directories referred to in fixtures.conftest" need to have __init__.py files for the plugins to be loaded by pytest

这里可以看到类似的案例:https://stackoverflow.com/a/54736237/6655459

关于python - 单独目录中的 pytest fixtures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567306/

相关文章:

python - 如何检查列表列表中的每个列表是否长度相同

unit-testing - Jacoco 检查与 jacoco 报告中的不同覆盖率值

python-3.x - Pytest 使用装置比较几个类实例

unit-testing - 使用 pygobject3 和 pytest 测试 GUI 应用程序

python - 有没有办法防止解析从另一个页面重定向的响应?

python - 如何分组 ndarray?

python - 如何让 pip3 在 Linux 上运行

unit-testing - 我可以调用嵌套函数进行单元测试吗?

visual-studio - 测试管理器但没有 TFS 访问权限

python - Pytest:获取所有测试的地址