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.
- Define a function called
test_delete_expense. - Inside it, create an expense with
client.post("/expenses", json=...)and extract itsidwithexpense_id = response.json()["id"]. - Call
client.delete(f"/expenses/{expense_id}")and store the result inresponse. Assert thatresponse.status_codeequals200. - Now verify the expense is gone — call
client.get(f"/expenses/{expense_id}")and assert that the status code equals404.
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding