pytest "DID NOT RAISE" - pytest.raises Expected an Exception
A pytest.raises(...) block expected an exception that never came. Either the code stopped raising (a behavior change), the wrong exception type is expected, or a dependency upgrade changed what is thrown.
What this error means
A test fails with Failed: DID NOT RAISE <ExceptionType>. The asserted error did not occur - the code under test returned normally or raised a different exception than the one in pytest.raises.
def test_rejects_bad_input():
with pytest.raises(ValueError):
parse("ok") # no longer raises
E Failed: DID NOT RAISE <class 'ValueError'>Common causes
Behavior changed so nothing raises
The code (or a dependency) now accepts the input the test expected to reject, so no exception is raised and the assertion fails.
Wrong exception type or message expected
The code raises a different exception class (or the dependency switched the type on upgrade), so pytest.raises(X) does not match and reports DID NOT RAISE.
How to fix it
Decide whether the code or the test is wrong
- If the code should still reject the input, this is a real regression - fix the code.
- If the behavior change is intended, update the test to the new contract.
- On a dependency upgrade, check whether the raised exception type changed.
Match the actual exception
Assert the exception type the code really raises, and the message if it matters.
with pytest.raises(TypeError, match="expected str"):
parse(123)How to prevent it
- Keep tests aligned with the contract the code actually guarantees.
- Pin dependencies so an upgrade does not silently change raised exceptions.
- Use
match=to assert the specific error, catching type/message drift.