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

सामान्य Assert Methods

Assert methods वे exact phrases हैं जिनसे आप Django को बताते हैं 'यह true होना चाहिए, वरना test fail हो।'
Syntax
markup
self.assertEqual(a, b)
self.assertNotEqual(a, b)
self.assertTrue(expression)
self.assertFalse(expression)
self.assertIn(item, collection)
self.assertRaises(ExceptionType, function, argument)

assertEqual और assertNotEqual

ये दो values को equality या inequality के लिए compare करते हैं और किसी भी test suite में सबसे commonly उपयोग होने वाले assertions हैं।

उदाहरण: assertEqual and assertNotEqual

These compare two values for equality or inequality and are the most commonly used assertions in any test suite.

markup
class EqualityTests(TestCase):
    def test_addition(self):
        self.assertEqual(1 + 1, 2)

    def test_not_equal(self):
        self.assertNotEqual(1 + 1, 3)
{# 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. #}

assertTrue और assertFalse

ये जांचते हैं कि एक single expression True या False evaluate होता है, boolean flags और conditions के लिए उपयोगी।

उदाहरण: assertTrue and assertFalse

These check that a single expression evaluates to True or False, useful for boolean flags and conditions.

markup
class BooleanTests(TestCase):
    def test_is_active_true(self):
        self.assertTrue(1 > 0)

    def test_is_active_false(self):
        self.assertFalse(1 < 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. #}

assertIn और assertRaises

assertIn जांचता है कि एक value एक list, string, या queryset के अंदर मौजूद है, जबकि assertRaises जांचता है कि code call करने पर एक specific exception raise हो।

Note: assertRaises को with के साथ एक context manager के रूप में उपयोग किया जाना चाहिए, जो उस code को wrap करता है जिसे raise करना चाहिए।

उदाहरण: assertIn and assertRaises

assertIn checks that a value exists inside a list, string, or queryset, while assertRaises checks that calling code raises a specific exception.

markup
class OtherAssertTests(TestCase):
    def test_value_in_list(self):
        self.assertIn(3, [1, 2, 3])

    def test_division_by_zero_raises(self):
        with self.assertRaises(ZeroDivisionError):
            1 / 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. #}
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. assertEqual(a, b) के बजाय assertTrue(a == b) उपयोग करना — यह काम करता है, लेकिन कहीं कम useful failure message देता है।
  2. Floating point numbers को assertEqual से compare करना assertAlmostEqual के बजाय, जो rounding से flaky failures का कारण बनता है।
  3. यह भूल जाना कि assertRaises को call को with block के अंदर wrap करना चाहिए, सिर्फ एक return value जांचना काफी नहीं।
चैप्टर सारांश
  • assertEqual(a, b) और assertNotEqual(a, b) दो values compare करते हैं।
  • assertTrue(x) और assertFalse(x) एक single boolean condition जांचते हैं।
  • assertIn(item, container) membership जांचता है, और assertRaises जांचता है कि एक exception raise हुआ।
  • सबसे specific assert method चुनना test टूटने पर सबसे साफ़ failure message देता है।

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.