0Pricing
Django Academy · درس

كتابة TestCase وعبارات التحقق

اختبار النماذج والعروض تلقائيًا

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

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

Why Write Tests?

Tests catch bugs before your users do. They let you change code with confidence, knowing a green run means nothing broke. ✅

Meet TestCase

Django gives you a TestCase base class. Subclass it and each method that starts with test_ becomes an automatic check.

from django.test import TestCase

Your First Test Class

Group related checks in one class. Here a TestCase subclass will hold all the tests for the Article model.

class ArticleTests(TestCase):
    pass

A Fresh Database

TestCase runs each test in its own transaction and rolls it back afterward. Tests never pollute your real data or each other.

Writing a Test Method

Name every test method with a test_ prefix so Django's runner discovers and executes it for you.

def test_title_is_saved(self):
    ...

Assert Equality

The classic check is assertEqual. It passes when two values match and fails loudly when they differ.

self.assertEqual(article.title, 'Hello')

Assert True or False

For boolean conditions use assertTrue and assertFalse. They make intent crystal clear in your test.

self.assertTrue(article.is_published)

Setting Up Shared Data

Need the same object in many tests? Build it once in setUp, which Django runs before every test method.

def setUp(self):
    self.a = Article.objects.create(title='Hi')

Testing a Model Method

Verify your model behaves as designed. Create an object, call its method, then assert the result is what you expect.

self.assertEqual(str(self.a), 'Hi')

Run the Tests

Kick everything off with the test command. Django finds your test files, runs them, and reports passes and failures.

python manage.py test

Reading the Output

A dot means a pass, an F means a failure, and E means an error. The summary shows exactly which test broke and why.

Quick Check

Django needs to know which methods are tests.

Recap

You wrote a TestCase, used setUp for shared data, checked results with assertEqual and assertTrue, and ran it all with manage.py test. 🎉

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

هل درس «كتابة TestCase وعبارات التحقق» مجاني؟

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

ماذا ستتعلم في «كتابة TestCase وعبارات التحقق»؟

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

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

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

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

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

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

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

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

  1. فئة Paginator
  2. عناصر التحكم بالصفحات في ListView
  3. كتابة TestCase وعبارات التحقق
  4. Test Client و Fixtures
← العودة إلى Django Academy