Why Testing: Assertions
beginner10 min readLesson 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.