在pytest中为参数化测试运行测试之前,仅运行安装程序一次

2024-03-29 05:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用夹具为测试设置资源,该测试应该在测试开始前创建一次资源,但测试是参数化的。如果我使用下面提到的方法,它为xx和yy的每一个组合调用夹具,有人能帮我找到另一种方法来实现这一点吗

@pytest.mark.usefixtures('create_files')
@pytest.mark.parametrize('xx', ['a', 'b', 'c'])
@pytest.mark.parametrize('yy', ['1', '2', '3'])
def test_operaration(self):
    .
    .
    .
    .

还有什么方法可以为每次运行将xx和yy的值传递给create_filesfixture ?


Tags: 方法test参数pytestdefcreatefiles资源
2条回答

create_files装置的范围设置为session

@pytest.fixture(scope='session')
def create_files():
    ...

最后,这有助于解决我的问题:

@pytest.fixture(scope='function')
def create_files(request: FixtureRequest):
    xx = request.getfixturevalue('xx')
    yy = request.getfixturevalue('yy')
    ...

相关问题 更多 >