Common Assert Methods
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.
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.
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.
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. #}
- Using assertTrue(a == b) instead of assertEqual(a, b) — it works, but produces a far less useful failure message.
- Comparing floating point numbers with assertEqual instead of assertAlmostEqual, causing flaky failures from rounding.
- Forgetting that assertRaises must wrap the call inside a with block, not just check a return value.
- 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.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: