我为setter方法编写了pytest测试,以检查其拒绝无效输入的能力。
def test_set_x_invalid(out):
with pytest.raises(ValueError):
out.x = -1
# No change to previously set value when input rejected
assert(out.x == 30)
被测方法正确引发ValueException
,pytest.raises
捕获并解释为通过。
但是,当我在列表中放置几个错误的输入,然后使用for循环访问列表元素时,pytest.raises
抱怨没有引发异常
def test_set_x_invalid(out):
shouldReject = [-1, 31, 87123761246, "foo", None, True, False, ";oaisdfhluhxcvlisd"]
for badx in shouldReject:
with pytest.raises(ValueError):
> out.x = badx
E Failed: DID NOT RAISE <class 'ValueError'>
这是否意味着raises
不能在循环主体中使用?