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.
pytest
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.
tests/test_parse.py
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.
Related guides
pytest "Fixture X called directly" in CIFix pytest "Fixture X called directly. Fixtures are not meant to be called directly" in CI - a test invoked a…
pydantic "ValidationError" in tests in CIFix pydantic "ValidationError" in tests in CI - data did not match the model schema, often from a v1-to-v2 be…
pytest "cannot collect test class because it has a __init__" in CIFix pytest "cannot collect test class X because it has a __init__ constructor" in CI - pytest skips test clas…