This custom pylint rule disallows calling pytest.raises
without a match
argument
to avoid capturing unintended exceptions and eliminate false-positive tests.
Suppose we want to test this function throws Exception("bar")
when condition2
is satisfied.
def func():
if condition1:
raise Exception("foo")
if condition2:
raise Exception("bar")
def test_func():
with pytest.raises(Exception):
func()
- This test passes when
condition1
is unintentionally satisfied. - Future code readers will struggle to identify which exception
pytest.raises
should match.
def test_func():
with pytest.raises(Exception, match="bar"):
func()
- This test fails when
condition1
is unintentionally satisfied. - Future code readers can quickly identify which exception
pytest.raises
should match by searchingbar
.
def test_func():
with pytest.raises(Exception): # pylint: disable=pytest-raises-without-match
func()