pytest:通过固定装置参数化测试用例
pytest: Parameterized test cases via fixtures
如何编写 yields/returns 参数化测试参数的夹具(方法)?
比如我有一个测试如下:
@pytest.mark.parametrize(
"input,expected",
[("hello", "hello"),
("world", "world")])
def test_get_message(self, input, expected):
assert expected == MyClass.get_message(input)
我对以下方法感兴趣,而不是通过 @pytest.mark.parametrize
传递 input
和 expected
:
@pytest.fixture(scope="session")
def test_messages(self):
# what should I write here to return multiple
# test case with expected value for each?
pass
def test_get_message(self, test_messages):
expected = test_messages["expected"] # somehow extracted from test_messages?
input = test_messages["input"] # somehow extracted from test message?
assert expected == MyClass.get_message(input)
要将参数移动到夹具中,您可以使用夹具参数:
@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
对我来说,你似乎想要 return 一个字典数组:
@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
但我不确定这是否是最好的方法,因为它仍然被认为只是一个测试用例,因此它将在 output/report.
中仅显示为一个测试用例。
如何编写 yields/returns 参数化测试参数的夹具(方法)?
比如我有一个测试如下:
@pytest.mark.parametrize(
"input,expected",
[("hello", "hello"),
("world", "world")])
def test_get_message(self, input, expected):
assert expected == MyClass.get_message(input)
我对以下方法感兴趣,而不是通过 @pytest.mark.parametrize
传递 input
和 expected
:
@pytest.fixture(scope="session")
def test_messages(self):
# what should I write here to return multiple
# test case with expected value for each?
pass
def test_get_message(self, test_messages):
expected = test_messages["expected"] # somehow extracted from test_messages?
input = test_messages["input"] # somehow extracted from test message?
assert expected == MyClass.get_message(input)
要将参数移动到夹具中,您可以使用夹具参数:
@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
对我来说,你似乎想要 return 一个字典数组:
@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
但我不确定这是否是最好的方法,因为它仍然被认为只是一个测试用例,因此它将在 output/report.
中仅显示为一个测试用例。