【问题标题】:Pytest: Inherit fixture parametersPytest:继承夹具参数
【发布时间】:2020-04-22 21:16:03
【问题描述】:

我有一个固定装置,可用作参数化测试以在两种状态(在线/离线)下运行的开关。如果测试处于离线模式,这还会将自定义标记应用于测试。

@pytest.fixture(
    params=['online', pytest.param('offline', marks=pytest.mark.jira('388', '828', '833', '918'))]
)
def network(request):
    """ A switch parameter that parametrizes test for testing online and offline functionality. """
    return request.param

我有一个测试,我希望将其他参数附加到 如果该测试也在离线模式下运行。由于我没有使用network 夹具,因此不包括标记(但我希望它们包括在内)。

@pytest.mark.smoke
@pytest.mark.jira('387', '772', '1009')
@pytest.mark.parametrize('network', ['online', pytest.param('offline', marks=pytest.mark.jira('1036'))])
def test_crud(network):
    ...

我的问题是,如何将夹具标记和@pytest.mark.parametrize 标记应用于测试?

【问题讨论】:

    标签: python parameters pytest fixtures


    【解决方案1】:

    我通过添加解决了这个问题

    def pytest_runtest_setup(item):
        if item.callspec.params.get('network') == 'offline':
            item.add_marker(pytest.mark.jira('388', '828', '833', '918'))
    

    conftest.py 并删除network 固定装置,以便将@pytest.mark.parametrize('network', ...) 添加到每个测试中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2023-03-03
      • 1970-01-01
      • 2022-12-08
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多