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

TestCase Class की मूल बातें

TestCase एक special toolbox है जो Django आपको देता है जो हर test के लिए एक fake database setup और clean up करना पहले से जानता है।
Syntax
markup
from django.test import TestCase

class ClassNameTests(TestCase):
    def setUp(self):
        # runs before each test
        ...

    def test_name(self):
        self.assertEqual(actual, expected)

TestCase को Subclass करना

Django की TestCase class Python की built-in unittest.TestCase को extend करती है और database transaction handling जोड़ती है, इसलिए एक test में किए changes कभी दूसरे test में leak नहीं होते।

Note: हमेशा TestCase को django.test से import करें, unittest से सीधे नहीं।

उदाहरण: Subclassing TestCase

Django's TestCase class extends Python's built-in unittest.TestCase and adds database transaction handling, so changes made in one test never leak into another test.

markup
from django.test import TestCase


class ExampleTests(TestCase):
    def test_true_is_true(self):
        self.assertTrue(True)
{# 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. #}

setUp Method

setUp() class के हर single test method से पहले अपने आप चलता है। यह वे objects बनाने की जगह है जिनकी कई tests को ज़रूरत है, repetition से बचते हुए।

Note: हर test में object creation copy-paste करने के बजाय shared fixtures के लिए setUp() उपयोग करें।

उदाहरण: The setUp method

setUp() runs automatically before every single test method in the class. It is the place to create objects that multiple tests need, avoiding repetition.

markup
from django.test import TestCase
from django.contrib.auth.models import User


class UserTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('alice', password='pass123')

    def test_user_created(self):
        self.assertEqual(self.user.username, 'alice')
{# 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. #}

Test Methods को Naming देना

Django का test runner सिर्फ उन methods को discover करता है जिनके नाम test से शुरू होते हैं। check_user() नाम का एक method चुपचाप कभी नहीं चलेगा।

Warning: test से शुरू न होने वाले methods चुपचाप skip होते हैं, errors के रूप में flag नहीं होते।

उदाहरण: Naming test methods

Django's test runner only discovers methods whose names start with test. A method named check_user() would silently never run.

markup
class NamingTests(TestCase):
    def test_username_is_lowercase(self):
        self.assertEqual('alice', 'alice'.lower())
{# 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. Django की TestCase के बजाय Python की plain unittest.TestCase से inherit करना, tests के बीच automatic database rollback खोते हुए।
  2. यह भूल जाना कि हर test method independently चलता है, फिर किसी पिछले test method से बची state पर depend करना।
  3. हर single test method के अंदर expensive setup code रखना बजाय setUp() में।
चैप्टर सारांश
  • django.test.TestCase हर test को एक database transaction में wrap करता है जो बाद में rolled back होता है।
  • आपकी लिखी हर test class को TestCase से subclass करना चाहिए, plain unittest.TestCase से नहीं।
  • Discover और run होने के लिए test methods को test शब्द से शुरू होना चाहिए।
  • setUp() हर test method से पहले चलता है, हर test को एक clean, predictable starting point देते हुए।

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.