Python pytest.jsp使用错误的异常引发传递
在下面的示例中,当Python pytest.jsp使用错误的异常引发传递,python,unit-testing,testing,automated-tests,pytest,Python,Unit Testing,Testing,Automated Tests,Pytest,在下面的示例中,当expecting=NotImplementedError时,我希望测试失败 import pytest def fun(): raise ValueError() @pytest.mark.parametrize("expecting", [ (ValueError), (NotImplementedError) ]) def test_something( expecting): with pytest.raises(ValueError)
expecting
=NotImplementedError
时,我希望测试失败
import pytest
def fun():
raise ValueError()
@pytest.mark.parametrize("expecting", [
(ValueError),
(NotImplementedError)
])
def test_something( expecting):
with pytest.raises(ValueError):
fun()
但是,相反,它通过了:
test_something[ValueError] PASSED
test_something[NotImplementedError] PASSED
为什么会出现这种行为,正确的用法是什么?您的测试没有对
进行任何处理。您使用pytest.raises(ValueError)编写了,因此pytest总是在寻找ValueError
,这就是fun()
引发的问题。也许你是想用pytest.raises编写(期望):
而不是?谢谢,我完全错过了。。我一直在关注pytest的例子——这个社区可能会很残酷!