This rule raises an issue when a test for an expected exception contains multiple method calls, making it unclear which call is expected to raise the exception.
When writing tests for exception handling, clarity is essential. If you chain multiple method calls within the code being tested for an exception, it becomes ambiguous which specific call is expected to raise that exception.
For example, both parse_payment(payload) and normalize() could raise a ValueError:
with pytest.raises(ValueError):
parse_payment(payload).normalize()
The test does not make it explicit which method is expected to raise the exception. This ambiguity makes the test harder to read and maintain.
By isolating the method call you want to test, you make your test explicit and unambiguous. This improves readability and maintainability, and makes your test suite a better specification of expected behavior.
Writing tests with multiple method calls when testing for exceptions reduces test quality:
The rule does not flag the following patterns even when they appear inside the exception-testing block (pytest.raises() or
assertRaises()), because they are not considered separate invocations under test:
ClassName().method() counts as a single invocation — the no-arg
constructor merely creates the receiver object and is unlikely to raise.do_work(json.dumps({"key": "value"})) counts as a single invocation — the
imported call is treated as setup data, not the expression under test.getattr, operator.attrgetter, operator.methodcaller,
functools.partial): only the final resulting call is counted.
import json
import pytest
def test_check_rejects_dangerous_command():
with pytest.raises(SecurityError): # Compliant — SomePolicy() is the receiver, not under test
SomePolicy().check(args)
def test_do_work_rejects_json_string():
with pytest.raises(InvalidPayloadError): # Compliant — json.dumps() is imported setup data
do_work(json.dumps({"key": "value"}))
def test_process_by_name_rejects_invalid_input():
item = get_item()
with pytest.raises(InvalidInputError): # Compliant — getattr() resolves the callable, process() is under test
getattr(item, "process")(invalid_input)
When using pytest’s pytest.raises() context manager, extract intermediate method calls into separate variables before the assertion.
This ensures only the final method call that you want to test is inside the exception-testing block.
import pytest
def test_normalize_rejects_negative_amount():
with pytest.raises(ValueError): # Noncompliant
parse_payment(payload).normalize()
import pytest
def test_normalize_rejects_negative_amount():
payment = parse_payment(payload)
with pytest.raises(ValueError):
payment.normalize()
When using unittest’s assertRaises() method, extract intermediate method calls before the assertion. This makes it clear which
specific method is being tested for the exception.
import unittest
class TestPayment(unittest.TestCase):
def test_normalize_rejects_negative_amount(self):
self.assertRaises(ValueError, lambda: parse_payment(payload).normalize()) # Noncompliant
import unittest
class TestPayment(unittest.TestCase):
def test_normalize_rejects_negative_amount(self):
payment = parse_payment(payload)
self.assertRaises(ValueError, payment.normalize)