ตรวจสอบเส้นทางและ JSON
ตรวจสอบรหัสสถานะและเนื้อหาการตอบกลับ
ตรวจสอบเส้นทางและ JSON เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 == 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. ✅
คำถามที่พบบ่อย
บทเรียน “ตรวจสอบเส้นทางและ JSON” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตรวจสอบเส้นทางและ JSON” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตรวจสอบเส้นทางและ JSON”
ตรวจสอบรหัสสถานะและเนื้อหาการตอบกลับ คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ตรวจสอบเส้นทางและ JSON” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ไคลเอนต์ทดสอบและอุปกรณ์ประกอบการทดสอบ
- ตรวจสอบเส้นทางและ JSON
- แยกการทดสอบด้วยฐานข้อมูลทดสอบ
- ทดสอบปลายทางที่ต้องยืนยันตัวตน