如何覆盖pytest固定装置,但仍然能够访问它?

问题描述 投票:1回答:1

我有一个conftest.py和一个插件,都用不同的实现定义了相同的灯具:

conftest.py

import pytest
@pytest.fixture
def f():
    yield 1

插件

import pytest
@pytest.fixture
def f():
    yield 2

[安装插件时,conftest仍会覆盖该插件,因此测试文件将仅显示conftest固定装置,即

test_.py

def test(f):
    assert f == 1 # True

我希望能够做这样的事情:

  1. 如果未安装该插件,请继续
  2. 否则,从conftest插件中获得插件固定装置的值

我设法成功了一半:

conftest.py

import pytest
@pytest.fixture
def f(pytestconfig):
    if pytestconfig.pluginmanager.has_plugin(plugin_name):
        # now what? I have get_plugin and import_plugin, but I'm not able to get the fixture from there...
python python-3.x pytest fixtures
1个回答
0
投票

我看到的最简单的方法是尝试获取插件的固定值。如果灯具查找失败,则没有插件定义它,您可以做自己的事。示例:

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