Test Delete Expense
Exit

Test Delete Expense

Write a test for deleting an expense and verifying it is gone

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Verifying a side effect

The delete test is different from the tests you have written so far. A GET test checks that the response contains the right data. A DELETE test checks that the data is gone after the request.

The simplest way to verify deletion: try to GET the deleted expense and confirm the API returns a 404 status code. This tests both the delete operation and the error handling in a single test.

Instructions

Add a test for the delete endpoint.

  1. Define a function called test_delete_expense.
  2. Inside it, create an expense with client.post("/expenses", json=...) and extract its id with expense_id = response.json()["id"].
  3. Call client.delete(f"/expenses/{expense_id}") and store the result in response. Assert that response.status_code equals 200.
  4. Now verify the expense is gone — call client.get(f"/expenses/{expense_id}") and assert that the status code equals 404.