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

Common Assert Methods

Assert methods are the exact phrases you use to tell Django 'this must be true, or the test fails.'

assertEqual and assertNotEqual

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

Example: 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 and assertFalse

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

Example: 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 and assertRaises

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

Note: assertRaises must be used as a context manager with with, wrapping the code that should raise.

Example: 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. #}
Common Mistakes
  1. Using assertTrue(a == b) instead of assertEqual(a, b) — it works, but produces a far less useful failure message.
  2. Comparing floating point numbers with assertEqual instead of assertAlmostEqual, causing flaky failures from rounding.
  3. Forgetting that assertRaises must wrap the call inside a with block, not just check a return value.
Chapter Summary
  • assertEqual(a, b) and assertNotEqual(a, b) compare two values.
  • assertTrue(x) and assertFalse(x) check a single boolean condition.
  • assertIn(item, container) checks membership, and assertRaises checks that an exception is raised.
  • Picking the most specific assert method gives the clearest failure message when a test breaks.

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.