pytest固定装置在单独的目录中 [英] pytest fixtures in a separate directory

查看:74
本文介绍了pytest固定装置在单独的目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求创建一个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.py. conftest.py仅包含包装器,以返回每个用@pytest.fixture注释的灯具的实例.当conftest.pyfixture_*.pytest_*.py文件都在同一目录中时,将夹具与测试一起使用是没有问题的.

但是,当固定装置放在子目录中时,我从pytest fixture 'cifs' not foundavailable fixtures: ...收到错误.我还没有找到任何文档说明如何将固定装置放置在test_*.pyconftest.py的附近,但没有任何迹象表明这也不起作用.

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

解决方案

请在您的 conftest.py

中添加以下内容

   import pytest

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

这确保了pytest

将找到在fixtures/下声明的所有固定装置.

请注意,在 fixtures.conftest"需要具有__init__.py文件,才能由pytest

加载插件

在这里可以看到类似的情况: https://stackoverflow.com/a/54736237/6655459

I'm looking to create a pytest structure where I can separate the fixtures from the tests completely. The reason for this separation is that I want to include the fixtures directory as an external item in subversion and share it between multiple projects.

tree of desired structure

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

I want to implement the functionality for each fixture in a separate file in order to avoid a single huge conftest.py. conftest.py would just include wrappers to return an instance of each fixture annotated with @pytest.fixture. There is no problem using a fixture together with a test when the conftest.py, fixture_*.py and test_*.py files are all in the same directory.

However, when the fixtures are separated in a subdirectory I get an error from pytest fixture 'cifs' not found, available fixtures: .... I haven't found any documentation explaining how to place fixtures outside of test_*.py or the conftest.py adjacent to test_*.py, but nothing to indicate that this shouldn't work either.

How can I place fixtures in their own subdirectory when using pytest?

解决方案

Please add the following in your conftest.py

  import pytest

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

This ensures that all fixtures declared under fixtures/ will be found by 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

A similar case can be seen here: https://stackoverflow.com/a/54736237/6655459

这篇关于pytest固定装置在单独的目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆