0Pricing
Flask Academy · درس

التحقق من المسارات وJSON

تحقق من رموز الحالة وأجسام الاستجابة.

التحقق من المسارات وJSON درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 == 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. ✅

الأسئلة الشائعة

هل درس «التحقق من المسارات وJSON» مجاني؟

نعم — نص درس «التحقق من المسارات وJSON» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحقق من المسارات وJSON»؟

تحقق من رموز الحالة وأجسام الاستجابة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «التحقق من المسارات وJSON»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. عميل الاختبار والتركيبات
  2. التحقق من المسارات وJSON
  3. عزل الاختبارات باستخدام قاعدة بيانات اختبار
  4. اختبار نقاط النهاية التي تتطلب المصادقة
← العودة إلى Flask Academy