pytest-raises-too-broad (PT011)
源自 flake8-pytest-style linter。
作用
检查没有 match
参数的 pytest.raises
调用。
为什么这不好?
pytest.raises(Error)
将捕获任何 Error
,并且可能捕获与被测代码无关的错误。为避免这种情况,应该使用 match
参数调用 pytest.raises
。需要 match
参数的异常名称可以通过 lint.flake8-pytest-style.raises-require-match-for
和 lint.flake8-pytest-style.raises-extend-require-match-for
设置进行配置。
示例
import pytest
def test_foo():
with pytest.raises(ValueError):
...
# empty string is also an error
with pytest.raises(ValueError, match=""):
...
建议改为
Options (选项)
lint.flake8-pytest-style.raises-require-match-for
lint.flake8-pytest-style.raises-extend-require-match-for