假设我有一个测试函数,它将参数化 record
作为字典,其中一个值是一个已经定义的 fixture 。
例如,我们有一个 fixture :
@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)
现在,我知道这可以通过将 fixture 传递给测试函数并相应地更新记录来解决,例如:
@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 ,虽然它似乎并不完全适合我的情况。无法从那里的答案中找到正确的用法。
最佳答案
一个解决方案是创建 record
作为固定装置而不是使用 parametrize
并接受 a_value
作为参数:
@pytest.fixture
def record(a_value):
return {
'a': a_value,
'other': 'other_value',
}
关于python - 在 pytest.mark.parametrize 中使用 fixture ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54078436/