pytest Failed: DID NOT RAISE
Fixes pytest's 'Failed: DID NOT RAISE' by checking the raises block placement, exception type, and upstream swallowing. Use when pytest.raises reports the expected exception never came. Not for wrong-exception failures.
TL;DR
pytest.raises failed because the code inside the with block never raised the expected exception. Check that the raising call is actually inside the block, that no try/except upstream swallows it, and that the exception type matches. Add match= when you also want to assert on the message.
Error
FAILED tests/test_payments.py::test_refund_rejected - Failed: DID NOT RAISE ValueErrorSteps
- Read the failure line: it names the expected exception that never came. Expected: you know exactly which
pytest.raisesblock to inspect. - Confirm the raising call sits inside the
withblock, not before it or behind an early return. Expected: the call that should raise is lexically inside the block. - Check the code under test does not swallow the exception with its own
try/except, and that the trigger condition is actually hit (right inputs, right state). Expected: a manual repro raises the exception outside pytest. - Verify the exception type:
pytest.raisesalso matches subclasses, so a mismatch means a different exception family or none at all. Expected: the raised type matches the expected type or a subclass of it. - Tighten the assertion with
match:with pytest.raises(ValueError, match="insufficient funds"):. Expected: the test now checks the message too, catching raises for the wrong reason.
When to use
- The failure is exactly "Failed: DID NOT RAISE" naming an exception.
- You are testing error paths with
pytest.raises. - A refactor changed which exception a function raises.
When not to use
- The code raises a different exception (the test fails with that exception, not DID NOT RAISE).
- You are asserting warnings; that is
pytest.warns, a different API. - The exception is raised asynchronously; async raises need the async test patterns for your plugin.
Tool compatibility
- pytest 7 and 8.
pytest.raiseswith thematch(regex) parameter.- Pytest-specific; unittest
assertRaisesis only analogous.
Variant phrasings
pytest did not raise expected exception
Same failure. The with pytest.raises(...) block completed without the exception.
pytest.raises not catching exception
Usually the exception type mismatches, or the raise happens outside the block.
Why it happens
pytest.raises is a context manager that only passes if its block raises. When the block finishes quietly, pytest reports DID NOT RAISE. The usual causes are the raising call placed outside the block, an upstream try/except swallowing the error, or the trigger condition not being met by the test inputs.
Edge cases
matchis a regex, so special characters in the expected message need escaping; usere.escapefor literal messages.- Expecting too broad a type (like
Exception) makes the test pass for the wrong reasons; expect the narrowest type the code raises. - When the code raises inside a thread or callback,
pytest.raisesaround the outer call will not see it; restructure so the raise is observable.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_DX1VJ3RpigD-nflfakfvtQ