Flask Academy · 课时

断言路由与 JSON

检查状态码和响应正文

第 2 / 4 课13 个步骤

断言路由与 JSON 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 == 200

Read 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 body

Assert 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.data

Parse 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 data

Check 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_type

Test 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 == 404

Test 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 == 201

Assert 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'] == 1

One 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. ✅

免费开始

用 AI 导师学习 Python — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「断言路由与 JSON」课时是免费的吗?

是的 — 「断言路由与 JSON」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「断言路由与 JSON」这节课中我会学到什么?

检查状态码和响应正文 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「断言路由与 JSON」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 测试客户端与测试夹具
  2. 断言路由与 JSON
  3. 使用测试数据库隔离测试
  4. 测试需要身份验证的端点
← 返回 Flask Academy