Periksa Rute dan JSON
Periksa kode status dan isi response.
Periksa Rute dan JSON adalah pelajaran Flask Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Flask Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Flask Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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. ✅
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Periksa Rute dan JSON” gratis?
Ya — teks lengkap “Periksa Rute dan JSON” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Flask Academy, upgrade ke CoddyKit PRO. Kursus Flask Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Periksa Rute dan JSON”?
Periksa kode status dan isi response. Kamu berlatih Flask Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Flask Academy?
Tidak diperlukan pengalaman sebelumnya. Flask Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Periksa Rute dan JSON” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Flask Academy ini?
Ya. Setiap pelajaran Flask Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Klien Pengujian dan Fixture
- Periksa Rute dan JSON
- Pisahkan Pengujian dengan Basis Data Pengujian
- Uji Titik Akhir yang Terautentikasi