Test Client و Fixtures
محاكاة الطلبات وتهيئة بيانات الاختبار
Test Client و Fixtures درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Testing Whole Views
Model tests are great, but you also want to test full pages. The test client lets you simulate a real browser request inside a test. 🌐
The Client Is Ready
Every TestCase gives you a self.client for free. No setup needed, it is wired up and waiting in each test.
Make a GET Request
Call self.client.get with a URL to fetch a page just like a browser would, then inspect what comes back.
response = self.client.get('/articles/')Check the Status Code
The response carries a status_code. Assert it equals 200 to confirm the page loaded successfully.
self.assertEqual(response.status_code, 200)A Cleaner Status Check
Django offers a shortcut. Use assertContains to confirm both a 200 status and that expected text is on the page.
self.assertContains(response, 'Latest Articles')Posting Form Data
Simulate a form submission with self.client.post, passing a dictionary of the fields a user would fill in.
self.client.post('/add/', {'title': 'Hi'})Test by Route Name
Hardcoded URLs break easily. Use reverse to look up the URL from its route name instead.
url = reverse('article-list')
response = self.client.get(url)What Are Fixtures?
A fixture is a file of pre-built rows. It seeds your test database so every test starts with the same known data.
Create a Fixture File
Export existing rows with dumpdata. Django writes them to JSON that you can replay later as a fixture.
python manage.py dumpdata blog > data.jsonLoad a Fixture in Tests
List your fixture files in the fixtures attribute and Django loads them automatically before each test runs.
class BlogTests(TestCase):
fixtures = ['data.json']Fixtures vs setUp
Reach for fixtures when you need a fixed dataset to load fast, and setUp when each test needs small, tailored objects.
Quick Check
You want to confirm a page loaded and shows a heading.
Recap
You drove views with the test client, checked status and content, posted form data, and seeded data with fixtures and dumpdata. 🎉
الأسئلة الشائعة
هل درس «Test Client و Fixtures» مجاني؟
نعم — نص درس «Test Client و Fixtures» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «Test Client و Fixtures»؟
محاكاة الطلبات وتهيئة بيانات الاختبار تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «Test Client و Fixtures»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- فئة Paginator
- عناصر التحكم بالصفحات في ListView
- كتابة TestCase وعبارات التحقق
- Test Client و Fixtures