Part 03 / Topic 3 of 6
Testing failure paths
Integration code spends half its life handling failure, so the failure paths need tests as much as the happy path:
def test_rejects_garbage():
with pytest.raises(ValueError):
parse_weight_kg("heavy")
def test_gives_up_after_three_retries(flaky_client):
with pytest.raises(SyncError):
fetch_all(flaky_client, max_retries=3)
pytest.raises(ValueError) asserts that the code inside the block raises ValueError — and fails the test if it does not. In Part 5 you will simulate timeouts, 401s and 503s with a mock transport and test the client's behaviour against each; the pattern is the same.
🧪 Quick quiz
- What does
pytest.raises(ValueError)check?
Show the answer
That the code inside raises ValueError — and the test fails if it does not. Testing failure paths matters as much as testing the happy path.
Done here? The practice set for Part 3 — 4 exercises with solutions — is on the part page.