[英]pytest parametrizing fixtures with multiple asserts
I have an example code where I have a parametrized test function with two asserts: 我有一个示例代码,其中有一个带有两个断言的参数化测试函数:
@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
So the output I wanted was (pseudo code): 所以我想要的输出是(伪代码):
assertion error 3==8
assertion error 5==8
assertion error 2==6
assertion error 4==6
While executing the test for all combinations is there a way to reach the second assert even if the first one fails ? 在对所有组合执行测试时,即使第一个失败,也有办法达到第二个断言吗?
As alternative I'd like to know is there a way to put this into class for example something similar to this: 作为一种替代,我想知道是否有一种方法可以将其放入类,例如类似于以下内容的方法:
@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
And I want to get the same output as the previous case: 我想获得与前面的案例相同的输出:
assertion error 3==8
assertion error 5==8
assertion error 2==6
assertion error 4==6
There is the pytest-expect plugin which does that kind of thing. 有pytest-expect插件可以做这种事情。
The way you outlined with using @pytest.mark.parametrize
on a class works out-of-the-box, you just forgot self
. 在类上使用@pytest.mark.parametrize
概述的方法是开箱即用的,您只是忘了self
。
Another possibility would be to simply write two tests and share the parametrization: 另一种可能性是仅编写两个测试并共享参数化:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.