简体   繁体   English

Pytest:继承治具参数

[英]Pytest: Inherit fixture parameters

I have a fixture that acts as a switch to parameterize tests to run in two states (online/offline).我有一个夹具充当开关来参数化测试以在两种状态(在线/离线)下运行。 This additionally applies custom marks to the test if the test is in offline mode.如果测试处于离线模式,这还会将自定义标记应用于测试。

@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

I have a test that I wish to append additional paramters to if the test is running in offline mode as well.我有一个测试,如果测试也在离线模式下运行,我希望 append 附加参数。 Since I'm not using the network fixture, the marks are not included (but I want them to be).由于我没有使用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):
    ...

My question is, how do I apply both the fixture marks and @pytest.mark.parametrize marks to the test?我的问题是,如何将夹具标记和@pytest.mark.parametrize标记应用于测试?

I solved this by adding我通过添加解决了这个问题

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

to conftest.py and removing the network fixture in favor of adding @pytest.mark.parametrize(.network', ...) to each test.conftest.py并删除network装置以支持将@pytest.mark.parametrize(.network', ...)添加到每个测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM