What is Django's TestCase and how is it different from Python's unittest.TestCase? Django का TestCase क्या है और यह Python के unittest.TestCase से कैसे अलग है?
Django's django.test.TestCase extends Python's standard unittest.TestCase and adds Django-specific features, most importantly wrapping every test method in a database transaction that is rolled back at the end of the test, so tests never leave residual data behind and can run in any order without interfering with each other.
It also provides a test client for simulating HTTP requests, assertion helpers like assertRedirects() and assertContains(), and integrates with Django's fixture loading. For tests that don't need a database at all, SimpleTestCase is a faster alternative that disables the transaction wrapping.
from django.test import TestCase
from .models import Product
class ProductModelTest(TestCase):
def test_str_representation(self):
product = Product.objects.create(name="Laptop", price=999)
self.assertEqual(str(product), "Laptop")Django का django.test.TestCase, Python के मानक unittest.TestCase को एक्सटेंड करता है और Django-विशिष्ट फीचर्स जोड़ता है, सबसे महत्वपूर्ण रूप से हर टेस्ट मेथड को एक डेटाबेस ट्रांज़ैक्शन में लपेटता है जिसे टेस्ट के अंत में रोलबैक किया जाता है, इसलिए टेस्ट कभी भी अवशिष्ट डेटा नहीं छोड़ते।
यह HTTP रिक्वेस्ट्स को सिम्युलेट करने के लिए एक टेस्ट क्लाइंट भी प्रदान करता है, assertRedirects() जैसे असर्शन हेल्पर्स, और Django के फिक्स्चर लोडिंग के साथ इंटीग्रेट होता है। बिल्कुल डेटाबेस की ज़रूरत न होने वाले टेस्ट के लिए, SimpleTestCase एक तेज़ विकल्प है।
from django.test import TestCase
from .models import Product
class ProductModelTest(TestCase):
def test_str_representation(self):
product = Product.objects.create(name="Laptop", price=999)
self.assertEqual(str(product), "Laptop")Was this answer clear?