pytest灯具在一个单独的目录中

问题描述 投票:4回答:3

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

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

但是,当灯具在子目录中分离时,我从pytest fixture 'cifs' not foundavailable fixtures: ...得到一个错误。我没有找到任何文件解释如何在test_*.py附近放置灯具或conftest.py附近的test_*.py,但没有任何迹象表明这也不起作用。

使用pytest时如何将灯具放在各自的子目录中?

python testing pytest fixtures
3个回答
1
投票

阅读here如何构建您的测试。

你的fixture目录似乎不在包中(项目没有__init__.py所以不能作为project.fixtures导入,因为fixtures不在路径中。你可以在你的tests/conftest.pysys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, "fixtures"))sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))路径中添加所需的dirs,具体取决于你想如何导入你的模块。


1
投票

请添加你的conftest.py

  import pytest

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

所有声明的额外灯具将由pytest创立

请注意,fixtures.conftest"中提到的各个目录需要有__init__.py文件才能由pytest加载插件

请在这里阅读类似的案例:https://stackoverflow.com/a/54736237/6655459


0
投票

你在import尝试过project/conftest.pying你的灯具吗?这就是“返回实例的包装器”的意思吗?

© www.soinside.com 2019 - 2024. All rights reserved.