Part 09 / Topic 6 of 7
Testing services
TestClient runs the whole HTTP stack in-process — no server, no ports, fast enough for CI. Test the contract, not the internals:
def test_contract():
assert c.get("/shipments/S-2001", headers=KEY).status_code == 200
assert c.get("/shipments/S-2001").status_code == 401
assert c.get("/shipments/NOPE", headers=KEY).status_code == 404
assert c.patch("/shipments/S-2001", headers=KEY, json={}).status_code == 422
These four asserts are the API's promise, written down. When the customer integrates against your service, this suite is what guarantees next month's refactor does not break them. If the customer's own system consumes your API, offer them the test file — a contract test both sides run is worth three pages of documentation.
Done here? The practice set for Part 9 — 4 exercises with solutions — is on the part page.