Testing an API
Prove your API works, and keeps working. You'll refresh pytest, send real requests to your app with TestClient, check status codes, headers and bodies, test every error, and give each test a fresh start. Then you'll write the tests that catch three bugs hidden in a concert venue's API.
Updated
Tests with pytest
- Your First Tests with pytest (Journeyman): A test is a small function that runs your code on an example and makes sure the answer is right, and pytest finds every test, runs it, and shows exactly what came back when one fails.
Testing an API End to End
- Testing an API with TestClient (Journeyman): FastAPI's TestClient hands a request straight to your app, with no server running and no port, so a pytest test can check the status code, a header and the JSON body that come back, for a GET and for a POST with a body.
- Testing Errors, with a Fresh Start for Every Test (Journeyman): Tests for an API's error answers check the status code, the whole error body and that nothing changed, @pytest.mark.parametrize runs one test for each bad value, and an autouse fixture that resets the data gives every test the same fresh start, so the tests pass alone and in any order.
Exercise: Concert Tickets API Tests
- Exercise: Concert Tickets API Tests (Journeyman): Write the TestClient tests for Starlight Stage's working tickets API, from its shows to buying tickets, its errors and refunds, and prove they're good by catching three bugs the check plants in copies of the API.
Up Next: Data with SQLite
- Up Next: Data with SQLite (Journeyman): Look back at what Module 2 taught you, see a project that puts all of it to work, an event sign-up API with a test for every rule, and see the gap Module 3 closes, by keeping an API's data in a SQLite database with SQL you write yourself.