← Back to Django Course | Chapter 13: Testing in Django | Lesson 8 of 9

Test Database का व्यवहार

Django आपके tests के लिए एक बिल्कुल नया, temporary practice database बनाता है, ताकि आपका real data कभी touch या break न हो।

एक separate test database क्यों होता है

अगर tests आपके real database में लिखते, तो वे real data corrupt कर सकते थे या junk rows पीछे छोड़ सकते थे। Django इसे suite शुरू होने से पहले एक throwaway database बनाकर solve करता है।

उदाहरण: Why a separate test database exists

If tests wrote to your real database, they could corrupt real data or leave behind junk rows. Django solves this by building a throwaway database before the suite starts.

bash
python manage.py test

⚠️ Run this in your own terminal or Node.js environment.

हर test के लिए automatic rollback

TestCase हर test method को एक transaction में wrap करता है और बाद में इसे rollback कर देता है, ताकि एक test में बनाए गए objects कभी दूसरे test में leak न हों।

उदाहरण: Automatic rollback per test

TestCase wraps each test method in a transaction and rolls it back afterward, so objects created in one test never leak into the next.

markup
from django.test import TestCase
from myapp.models import Book


class IsolationTests(TestCase):
    def test_one_creates_a_book(self):
        Book.objects.create(title='Temp Book')
        self.assertEqual(Book.objects.count(), 1)

    def test_two_starts_with_no_books(self):
        self.assertEqual(Book.objects.count(), 0)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

SQLite और test database

DATABASES[default] के रूप में SQLite के साथ, Django का test runner default रूप से test database को memory में बना सकता है, जिससे पूरी suite बिना किसी extra setup के तेज़ी से run होती है।

उदाहरण: SQLite and the test database

With SQLite as DATABASES[default], Django's test runner can create the test database in memory by default, making the whole suite run quickly with zero extra setup.

markup
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह चिंता करना कि tests run करने से real production/development data modify या delete हो जाएगा — Django का test database पूरी तरह अलग होता है और बाद में फेंक दिया जाता है।
  2. यह मान लेना कि एक test method में बनाया गया data दूसरे में visible है — हर test method के database changes अपने आप rollback होते हैं।
  3. यह न समझना कि test database वही engine उपयोग करता है जो DATABASES[default] (यहां SQLite) उपयोग करता है, इसलिए SQLite project के लिए कोई extra configuration ज़रूरी नहीं।
चैप्टर सारांश
  • Tests run करने से पहले, Django एक separate test database बनाता है, default रूप से test_ prefix के नाम से।
  • हर individual test method एक database transaction के अंदर run होता है जो test खत्म होने पर rollback हो जाता है।
  • पूरी suite खत्म होने के बाद, Django test database को अपने आप destroy कर देता है।
  • SQLite projects के लिए, test database पूरी तरह memory में भी run हो सकता है, जिससे tests तेज़ हो जाते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.