Python pytest具有多个断言的参数化夹具
我有一个示例代码,其中我有一个参数化测试函数和两个断言:Python pytest具有多个断言的参数化夹具,python,testing,pytest,Python,Testing,Pytest,我有一个示例代码,其中我有一个参数化测试函数和两个断言: @pytest.mark.parametrize("test_input,expected", [ ("3", 8), ("2", 6), ]) def test_eval(test_input, expected): assert test_input == expected # first assert assert test_input + 2 ==expected # second assert
@pytest.mark.parametrize("test_input,expected", [
("3", 8),
("2", 6),
])
def test_eval(test_input, expected):
assert test_input == expected # first assert
assert test_input + 2 ==expected # second assert
所以我想要的输出是(伪代码):
在对所有组合执行测试时,是否有一种方法可以达到第二个断言(即使第一个断言失败)
作为替代方案,我想知道是否有一种方法可以将其放入课堂,例如类似于以下内容:
@pytest.mark.parametrize("test_input,expected", [
("3", 8),
("2", 6),
])
class TestFunc(object):
def test_f1(test_input, expected):
assert test_input==expected
def test_f2(test_input, expected):
assert test_input+2==expected
我希望得到与前一种情况相同的输出:
assertion error 3==8
assertion error 5==8
assertion error 2==6
assertion error 4==6
有一个插件可以做这种事情
您在类上使用@pytest.mark.parametrize
所概述的方法是现成的,您只是忘记了self
另一种可能是编写两个测试并共享参数化:
eval_parametrize = pytest.mark.parametrize("test_input, expected", [
("3", 8),
("2", 6),
])
@eval_parametrize
def test_f1(test_input, expected):
assert test_input == expected
@eval_parametrize
def test_f2(test_input, expected):
assert test_input + 2 == expected
我想要一个不使用插件的解决方案(对不起,我没有提到),但无论如何,expect是一个很好的解决方案,我想,谢谢
eval_parametrize = pytest.mark.parametrize("test_input, expected", [
("3", 8),
("2", 6),
])
@eval_parametrize
def test_f1(test_input, expected):
assert test_input == expected
@eval_parametrize
def test_f2(test_input, expected):
assert test_input + 2 == expected