pytest-raises-with-multiple-statements (PT012)
源自 flake8-pytest-style linter。
作用
检查包含多个语句的 pytest.raises
上下文管理器。
此规则允许 pytest.raises
主体包含带有空主体的 for
循环(例如,pass
或 ...
语句),以测试迭代器行为。
为什么这不好?
当 pytest.raises
用作上下文管理器并包含多个语句时,可能会导致测试在实际应该失败时通过。
pytest.raises
上下文管理器应该只包含一个引发预期异常的简单语句。
示例
import pytest
def test_foo():
with pytest.raises(MyError):
setup()
func_to_test() # not executed if `setup()` raises `MyError`
assert foo() # not executed
建议改为