Inspect a captured exception contract
An exception test should check the failure the caller relies on. Capturing an exception with pytest.raises lets the test inspect its type and message instead of merely accepting any failure from the code block.
Keep the context manager around the operation expected to fail. Setup inside the same block might raise first and create a false positive. The match parameter is a regular expression, so escaping and anchoring matter when you intend exact text. An unexpected exception type should remain visible; it may reveal a programming error rather than a valid rejection.
import pytest
with pytest.raises(ValueError,
match="^negative$") as info:
raise ValueError("negative")
print(str(info.value))Your task
Implement captured_error(operation,pattern): call operation inside pytest.raises(ValueError,match=pattern), return its message. A missing error, wrong message or different exception type must not be accepted.
The explanation opens here once every check passes.