Using assertEqual and Other Assertions
Common assertions
Section titled “Common assertions”Equality
Section titled “Equality”self.assertEqual(actual, expected)
self.assertNotEqual(actual, expected)Truthiness
Section titled “Truthiness”self.assertTrue(condition)
self.assertFalse(condition)None checks
Section titled “None checks”self.assertIsNone(value)
self.assertIsNotNone(value)Identity / type
Section titled “Identity / type”self.assertIs(a, b)
self.assertIsInstance(obj, dict)Testing exceptions
Section titled “Testing exceptions”Use assertRaises for exception behavior.
import unittest
def parse_age(s: str) -> int:
age = int(s)
if age < 0:
raise ValueError("age must be >= 0")
return age
class TestParseAge(unittest.TestCase):
def test_negative_age_raises(self):
with self.assertRaises(ValueError):
parse_age("-1")Prefer specific assertions (e.g., assertIsNone) instead of assertTrue(x is None).
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Write a unittest TestCase
Section titled “Exercise 1 – Write a unittest TestCase”Exercise 2 – assertRaises
Section titled “Exercise 2 – assertRaises”Exercise 3 – setUp and tearDown
Section titled “Exercise 3 – setUp and tearDown”The assertion you choose decides what a failure tells you
Section titled “The assertion you choose decides what a failure tells you”Both of these test the same thing. Only one is useful when it breaks:
def test_assertTrue_says_nothing(self):
self.assertTrue([1, 2, 3] == [1, 2, 4])
def test_assertEqual_shows_a_diff(self):
self.assertEqual([1, 2, 3], [1, 2, 4])Measured output:
AssertionError: False is not trueAssertionError: Lists differ: [1, 2, 3] != [1, 2, 4]
First differing element 2:
3
4
- [1, 2, 3]
? ^
+ [1, 2, 4]
? ^assertTrue reduced the comparison to a boolean before the framework saw it, so all it
could report was False. assertEqual received both objects and produced a diff naming
the differing element.
flowchart TD A["assertTrue(a == b)"] --> B["Python evaluates a == b to False"] B --> C["unittest sees only False
-> 'False is not true'"] D["assertEqual(a, b)"] --> E["unittest receives BOTH objects"] E --> F["type-aware diff:
lists, dicts, sets, strings"]
The ones worth knowing
Section titled “The ones worth knowing”| assertion | use for |
|---|---|
assertEqual(a, b) | any equality — gives type-aware diffs |
assertAlmostEqual(a, b) | floats |
assertIn(a, b) | membership, with both operands reported |
assertIs(a, b) | identity — None, True, False |
assertIsNone(x) | clearer than assertEqual(x, None) |
assertRaises(Err) | as a context manager |
assertRaisesRegex(Err, "text") | the exception and its message |
assertCountEqual(a, b) | same items, order irrelevant |
Floats need assertAlmostEqual
Section titled “Floats need assertAlmostEqual”self.assertEqual(0.1 + 0.2, 0.3) # AssertionError: 0.30000000000000004 != 0.3
self.assertAlmostEqual(0.1 + 0.2, 0.3) # passesMeasured both. Binary floating point cannot represent 0.3 exactly, so exact equality is
the wrong question for any computed float.
Testing that something raises
Section titled “Testing that something raises”with self.assertRaises(ZeroDivisionError):
div(1, 0)
with self.assertRaisesRegex(ZeroDivisionError, "cannot divide"):
div(1, 0)The context-manager form is important: assertRaises(Err, div, 1, 0) also works but reads
badly and cannot wrap several statements. Prefer assertRaisesRegex when the message
matters — a test that accepts any ValueError will keep passing when the code starts
failing for an entirely different reason.
See it move
Section titled “See it move”Check yourself
Section titled “Check yourself”-
assertTrue([1,2,3] == [1,2,4]) fails. What does it print?
Python evaluated the comparison to False before unittest saw it, so False is all it could report. assertEqual receives both objects and produces a diff.
pch.quizShowAnswer
B — AssertionError: False is not true — Python evaluated the comparison to False before unittest saw it, so False is all it could report. assertEqual receives both objects and produces a diff.
-
Why does assertEqual(0.1 + 0.2, 0.3) fail?
Measured exactly that value. Use assertAlmostEqual for any computed float, which measured as passing.
pch.quizShowAnswer
B — binary floating point cannot represent 0.3 exactly, so the sum is 0.30000000000000004 — Measured exactly that value. Use assertAlmostEqual for any computed float, which measured as passing.
-
Why prefer assertRaisesRegex over assertRaises?
A test that accepts any ValueError will happily pass on an unrelated ValueError introduced later.
pch.quizShowAnswer
B — it checks the message too, so the test does not keep passing when the code starts failing for a different reason — A test that accepts any ValueError will happily pass on an unrelated ValueError introduced later.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading