pytest "Failed: DID NOT RAISE" in CI
A with pytest.raises(...) block asserts that the enclosed code raises a given exception. The code ran to completion without raising, so pytest fails the assertion with "DID NOT RAISE".
What this error means
A test fails with "Failed: DID NOT RAISE <class 'ValueError'>" - the behavior changed and no longer raises, or the expected exception type is wrong.
def test_rejects_empty():
with pytest.raises(ValueError):
parse("")
E Failed: DID NOT RAISE <class 'ValueError'>Common causes
The code no longer raises
A change made the function tolerate the input (or raise later), so the expected exception never occurs in the block.
The wrong exception type is expected
The code raises a different exception than the one in pytest.raises, so the expected type is not seen.
How to fix it
Confirm the real behavior and align the test
- Run the code under test with the input to see what it actually does.
- If it should raise, fix the code; if behavior changed intentionally, update the test.
- Match the exception type in
pytest.raisesto what is raised.
with pytest.raises(ValueError, match="empty"):
parse("")Keep the asserted call inside the block
Ensure the call that should raise is inside the with block, not before it.
How to prevent it
- Update raise-expectations when behavior intentionally changes.
- Assert the exception type and message with
match=. - Keep only the raising call inside the
pytest.raisesblock.