0Pricing
Flask Academy · درس

عزل الاختبارات باستخدام قاعدة بيانات اختبار

استخدم قاعدة بيانات مؤقتة لكل تشغيل للاختبارات.

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

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

Why Isolate the Database

Tests must never touch your real data. A test database keeps every run clean, so one test can never corrupt another or your production rows.

Use a Throwaway SQLite DB

The easy choice is an in-memory SQLite database. It lives only in RAM and vanishes the moment the test process ends.

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'

Flip on Testing Config

Set TESTING to True before tests run. It turns off error catching so exceptions surface clearly in your output.

app.config['TESTING'] = True

Build a Fresh Schema

Inside an app context, call db.create_all(). It builds every table from your models so each test run starts from an empty schema.

with app.app_context():
    db.create_all()

Tear Down After Tests

After the test, call db.drop_all() to remove every table. This guarantees the next run cannot inherit leftover rows.

with app.app_context():
    db.drop_all()

Put It in a Fixture

Wrap setup and teardown in one fixture. The code before yield creates the schema; the code after it drops everything cleanly.

@pytest.fixture
def app_db():
    db.create_all()
    yield
    db.drop_all()

Seed Sample Data

Many tests need a known row to read. Add a record in the fixture, then commit it so the test starts from a predictable state.

db.session.add(User(name='Ada'))
db.session.commit()

Why Fresh State Matters

Tests should pass in any order. Isolation means each test sees the same starting data, so results never depend on what ran before.

Roll Back Between Tests

An alternative to recreating tables is a rollback. Wrap each test in a transaction and undo it afterward for a fast, clean slate.

db.session.rollback()

Keep Test Config Separate

Store test settings in a dedicated TestConfig class. Your factory loads it only during tests, never in development or production.

class TestConfig:
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'

Never Test Against Prod

Always double-check the URI points at a test database. Pointing tests at production data is the fastest way to lose real records.

Quick Check

You want tests that never harm real data. What database do you use?

Recap: Test Database

You spin up a clean test database, seed known data, and tear it down after. Now your tests are fast, repeatable, and safe. 🗄️

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

هل درس «عزل الاختبارات باستخدام قاعدة بيانات اختبار» مجاني؟

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

ماذا ستتعلم في «عزل الاختبارات باستخدام قاعدة بيانات اختبار»؟

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

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

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

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

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

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

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

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

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