Assert on Routes and JSON
Check status codes and response bodies.
Assert on Routes and JSON is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What You Assert
A good route test checks two things: the right status code and the right body. If both match, you know the endpoint behaves as promised.
Check the Status Code
The response carries a status_code attribute. A healthy page returns 200, so assert on it to confirm the route loaded without error.
resp = client.get('/')
assert resp.status_code == 200Read the Raw Body
The full response body lives in resp.data as bytes. Decode it to text when you want to search for words inside an HTML page.
body = resp.data.decode()
assert 'Welcome' in bodyAssert Text Is Present
To confirm a page shows the right content, check that a phrase appears in the body. The simple in operator is perfect for this.
assert b'Welcome' in resp.dataParse a JSON Response
For API routes, call resp.get_json(). Flask parses the body into a Python dict or list so you can assert on real values.
data = resp.get_json()
assert data['name'] == 'Ada'Assert on Dict Keys
Once you have the parsed dict, check individual keys and values. This proves your serializer returned exactly the fields you expect.
data = resp.get_json()
assert data['id'] == 1
assert 'email' in dataCheck the Content Type
A JSON endpoint should advertise itself. Assert that resp.content_type includes application/json so clients parse it correctly.
assert 'application/json' in resp.content_typeTest a 404 Route
Error paths deserve tests too. Request a missing URL and assert the status is 404, proving your app fails gracefully.
resp = client.get('/nope')
assert resp.status_code == 404Test a POST Endpoint
Send data with client.post and a json argument. Then assert the created status code, usually 201, and the returned body.
resp = client.post('/items', json={'name': 'pen'})
assert resp.status_code == 201Assert on a JSON List
Collection endpoints return a list. Parse it, then assert its length or inspect items by index to verify the payload shape.
items = resp.get_json()
assert len(items) == 3
assert items[0]['id'] == 1One Behavior per Test
Keep each test focused on a single behavior. Small, named tests make failures obvious and your suite far easier to read.
Quick Check
You hit a JSON API route in a test. How do you read its body?
Recap: Routes and JSON
You now assert on status codes, page text, and parsed JSON. With these moves you can pin down any route's behavior with confidence. ✅
Frequently asked questions
Is the “Assert on Routes and JSON” lesson free?
Yes — the full text of “Assert on Routes and JSON” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Assert on Routes and JSON”?
Check status codes and response bodies. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Assert on Routes and JSON” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Test Client and Fixtures
- Assert on Routes and JSON
- Isolate Tests with a Test Database
- Test Authenticated Endpoints