首页 > 解决方案 > 将参数化夹具与 pytest 中的参数化测试相关联

问题描述

我想在 pytest 中做类似的事情(这只是一个简化的例子):

import pytest

@pytest.fixture(params=['A', 'B'])
def fixture1(request):
    return request.param.lower()

@pytest.mark.parametrize("extra_arg1", ["1", "2"])
def test_xxx(fixture1, extra_arg1):
    assert fixture1 + extra_arg1 == ..... 
    # the test is expected to run 4 times and the results should be "a1", "b1", "a2", "b2"

如何将参数化夹具与测试中的参数相关联?注意:我必须使用fixture,因为它已经在项目回购中到位。

谢谢!

标签: pythonunit-testingpytestfixtures

解决方案


推荐阅读