在pytest.mark.parametrize内部使用固定装置

时间:2019-01-07 16:43:30

标签: python python-2.7 testing pytest

说我有一个测试函数,该函数以参数record作为字典,其中一个值是已经定义的夹具。

例如,我们有一个灯具:

@pytest.fixture
def a_value():
    return "some_value"

和测试功能:

@pytest.mark.parametrize("record", [{"a": a_value, "other": "other_value"},
                                    {"a": a_value, "another": "another_value"}])
def test_record(record):
    do_something(record)

现在,我知道可以通过将固定装置传递给测试函数并相应地更新记录来解决此问题,例如:

@pytest.mark.parametrize("record", [{"other": "other_value"},
                                    {"another": "another_value"}])
def test_record(a_value, record):
    record["a"] = a_value
    do_something(record)

但是我想知道是否有没有这种“解决方法”的方法,当我已经定义了许多夹具,而我只想在传递给函数的每个参数化记录中使用它们时。

我已经检查了this question,尽管它似乎并不完全适合我的情况。从那里的答案中找不到正确的用法。

1 个答案:

答案 0 :(得分:0)

一种解决方案是创建record作为固定装置,而不是使用parametrize并接受a_value作为参数:

@pytest.fixture
def record(a_value):
    return {
        'a': a_value,
        'other': 'other_value',
    }