Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Why Testing: Assertions

โญ beginnerโณ 10 min read๐Ÿ“ Lesson 44 of 169

assert states what must be true; tests are asserts anyone can re-run.

assert states what must be true โ€” Python checks it for you:

total = 245
assert total > 0, "total must be positive"

If the condition holds, nothing happens. If not, an AssertionError with your message crashes the program AT THE LIE, not three steps later.

That idea scales into automated tests. You already used asserts as a learner; writing them yourself flips the perspective:

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0

test_add()          # silent = passing; loud = a bug with an address

A test suite is a list of functions like this that anyone can re-run. Silent output is the sound of confidence.

โšก Now practice

Ready to Code
Assertions & Make-the-Tests-PassRaise informative errors; satisfy boundary-hitting test suites.
3 challenges ยท ยท ~30 min