gpt4 book ai didi

python - 单独目录中的 pytest fixtures

转载 作者:太空狗 更新时间:2023-10-30 01:28:23 24 4
gpt4 key购买 nike

我正在寻求创建一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com