A. *_*rid 5 python testing pytest python-2.7
假设我有一个测试函数,它将参数化record作为字典,其中它的值之一是已经定义的固定装置。
例如,我们有一个固定装置:
@pytest.fixture
def a_value():
return "some_value"
Run Code Online (Sandbox Code Playgroud)
和测试功能:
@pytest.mark.parametrize("record", [{"a": a_value, "other": "other_value"},
{"a": a_value, "another": "another_value"}])
def test_record(record):
do_something(record)
Run Code Online (Sandbox Code Playgroud)
现在,我知道可以通过将夹具传递给测试函数并相应地更新记录来解决这个问题,例如:
@pytest.mark.parametrize("record", [{"other": "other_value"},
{"another": "another_value"}])
def test_record(a_value, record):
record["a"] = a_value
do_something(record)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一种方法可以在没有这种“解决方法”的情况下做到这一点,当我有许多已经定义的固定装置并且我只想在传递给函数的每个参数化记录中使用它们时。
我已经检查过这个问题,尽管它似乎并不完全适合我的情况。从那里的答案中找不到正确的用法。
一种解决方案是创建record为固定装置,而不是使用parametrize并接受a_value作为参数:
@pytest.fixture
def record(a_value):
return {
'a': a_value,
'other': 'other_value',
}
Run Code Online (Sandbox Code Playgroud)