Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest具有多个断言的参数化夹具_Python_Testing_Pytest - Fatal编程技术网

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