2 回答

TA貢獻1777條經驗 獲得超10個贊
要將參數移動到夾具中,您可以使用夾具參數:
@pytest.fixture(params=[("hello", "hello"),
("world", "world")], scope="session")
def test_messages(self, request):
return request.param
def test_get_message(self, test_messages):
input = test_messages[0]
expected = test_messages[1]
assert expected == MyClass.get_message(input)
您還可以將參數放入單獨的函數中(與 wih 相同parametrize),例如
def get_test_messages():
return [("hello", "hello"), ("world", "world")]
@pytest.fixture(params=get_test_messages(), scope="session")
def test_messages(self, request):
return request.param

TA貢獻1829條經驗 獲得超13個贊
對我來說,你似乎想返回一個字典數組:
@pytest.fixture(scope="session")
def test_messages():
return [
{
"input": "hello",
"expected": "world"
},
{
"input": "hello",
"expected": "hello"
}
]
要在測試用例中使用它,您需要遍歷數組:
def test_get_message(self, test_messages):
for test_data in test_messages:
input = test_data["input"]
expected = test_data["expected"]
assert input == expected
但我不確定這是否是最好的方法,因為它仍然被視為只有一個測試用例,因此它只會在輸出/報告中顯示為一個測試用例。
添加回答
舉報