0Pricing
Flask Academy · درس

عميل الاختبار والتركيبات

شغّل تطبيقًا وعميلًا للاختبارات.

عميل الاختبار والتركيبات درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Test Your Flask App

Tests let you change code without fear. With pytest you can prove every route still works in seconds, instead of clicking through your app by hand. 🧪

Meet the Test Client

Flask ships a fake browser called the test client. It calls your routes in memory, with no real server or network involved.

client = app.test_client()
resp = client.get('/')

Get a Client From Your App

You build the client by calling app.test_client(). It returns an object whose methods mirror real HTTP verbs like get and post.

from app import app
client = app.test_client()

Send a Request, Get a Response

Calling client.get('/') returns a response object. You inspect it just like a real one, but everything ran instantly inside Python.

resp = client.get('/')
print(resp.status_code)

What Is a Fixture

A fixture is reusable setup code pytest runs before a test. Instead of rebuilding the client everywhere, you define it once and reuse it.

Write a Client Fixture

Decorate a function with @pytest.fixture and yield a test client. Any test that names it as an argument receives a fresh client.

@pytest.fixture
def client():
    yield app.test_client()

Request the Fixture by Name

To use a fixture, simply add its name as a test argument. Pytest sees the match and injects the value for you automatically.

def test_home(client):
    resp = client.get('/')
    assert resp.status_code == 200

Yield vs Return in Fixtures

Using yield lets you run cleanup after the test finishes. Code before yield is setup; code after yield tears everything down.

@pytest.fixture
def client():
    yield app.test_client()
    print('cleanup here')

Where to Put Fixtures

Put shared fixtures in a file named conftest.py. Pytest finds it automatically, so every test file can use those fixtures with no import.

Naming Test Files

Pytest discovers files that start with test_ and functions named the same way. Stick to that pattern and your tests run with one command.

# file: test_routes.py
def test_home(client):
    ...

Run the Whole Suite

From your project root, just run pytest in the terminal. It collects every test file, runs them, and prints a clean pass or fail summary.

$ pytest
2 passed in 0.04s

Quick Check

You want a reusable test client. How do you do it?

Recap: Client and Fixtures

You met the in-memory test client and learned to share it with a fixture in conftest.py. Now every test starts with a ready client. 🎉

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

هل درس «عميل الاختبار والتركيبات» مجاني؟

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

ماذا ستتعلم في «عميل الاختبار والتركيبات»؟

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

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

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

كم من الوقت يستغرق درس «عميل الاختبار والتركيبات»؟

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

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

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

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

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