सामान्य Assert Methods
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.
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.
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 हो।
उदाहरण: 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.
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. #}
assertEqual(a, b)के बजायassertTrue(a == b)उपयोग करना — यह काम करता है, लेकिन कहीं कम useful failure message देता है।- Floating point numbers को
assertEqualसे compare करनाassertAlmostEqualके बजाय, जो rounding से flaky failures का कारण बनता है। - यह भूल जाना कि
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 देता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: