Learning outcomes
By the end of this lesson you should be able to: write a simple assert-based test for a function; describe why automated tests beat eyeballing; use print-instrumentation to see what a loop is really doing; and do hypothesis-driven debugging — forming a guess and testing it rather than changing code at random.
Prerequisites
Lesson 6 (debugging basics), Lesson 7 (functions) and Lesson 10 (decomposition). Testing makes the most sense once a program is split into functions, because each function is a unit you can test on its own.
The big idea
Lesson 6 showed you how to debug. This lesson makes it proactive: instead of waiting for a bug and then hunting it, you write checks that tell you the moment something stops being right. The deeply practical reason to test is that a function that works today may stop working after your next change. A test catches that. Eyeballing re-runs the whole program and relies on you noticing — a test is a permanent, instant, and precise reminder of what the function is supposed to do.
Why this matters before the syntax
The simplest way to write a test in Python is the assert statement. assert a function call equals an expected value; if it does, nothing happens and the program continues; if it does not, Python raises an AssertionError and stops. An assert is a way of writing down, in code, the statement "I believe this is true" — and having the computer verify it every time the program runs.
Testing works best on well-decomposed functions, which is exactly what lesson 10 taught you to write. A small function with a clear input and output is easy to test: pick an input, state the expected output. If a program is one giant blob, you cannot test part of it — you can only run the whole thing and look at the final result, which hides which piece misbehaved. This is the payoff of decomposition: it makes testing possible, and testing makes correctness possible.
Debugging in this framework becomes hypothesis-driven. When a test fails, you learn something before you change anything: which input produced the wrong output. That is a clue, and clues should narrow the search rather than invite a blind fix. Print-instrumentation — temporarily adding a print to see a variable's value — is the checkout tool. The discipline is: form a guess about the cause, test it with a targeted check, and only then change the code. This is the opposite of random-walk, the failure mode lesson 6 warned you about.
Worked example 1: a passing test
The simplest test. Write what the function should do, and have Python check it. If both asserts pass, nothing is printed — which is the point: silent success.
def add(a, b):
return a + b
assert add(2, 3) == 5, "add(2, 3) should be 5"
assert add(-1, 1) == 0, "add(-1, 1) should be 0"
print("all tests passed")all tests passedThe assert statement checks a condition. The message after the comma is what Python reports if the condition is False. Here both expectations hold — 2 + 3 is 5 and -1 + 1 is 0 — so both asserts pass silently and the program reaches the final print. Note that this tells you nothing new while the function is correct; it is insurance that pays off the moment you break it.
Worked example 2: a failing test surfaces the bug
Now the test is wrong about what the function should return, and Python tells you. Crucially, the error is caught here so we can read it as a message rather than a crash.
def add(a, b):
return a + b
try:
assert add(2, 3) == 6, "expected 6"
except AssertionError as e:
print("caught:", e)caught: expected 6The assert fails because 2 plus 3 is 5, not 6 — the expectation was wrong, not necessarily the function. The try/except is here only so we can show the message as text; in normal use, an uncaught AssertionError just stops the program at the failing line, which is precisely the signal you want: it tells you exactly which expectation did not hold, and the message is a note you wrote listing what you expected. That is a far more informative failure than "the output looked wrong".
Worked example 3: print-instrumentation
When a function misbehaves and you are not sure where, add a print to watch it. Here we watch a search loop as it checks each element.
def find(words, target):
for i, w in enumerate(words):
print("checking", i, w)
if w == target:
return i
return -1
print("found at", find(["a", "b", "c"], "b"))checking 0 a
checking 1 b
found at 1The print is temporary scaffolding. It reveals what the loop is actually doing — here, that it compares element 0, then element 1, and matches at index 1, so it immediately returns. Without this, you might wonder whether the loop is even reaching the target. Printing a variable just before it matters is the fastest way to turn a guess into a certainty about what the code does. Once you have fixed the problem, remove the print.
Common mistakes
Watch outOnly testing the 'happy path'. If a function is supposed to handle an empty list or a zero, test those too. The edge cases are where bugs hide.
Watch outLeaving print-instrumentation in production. Prints used for debugging are scaffolding — remove them once the bug is found, or leave them fenced behind a logging level.
Watch outRandom-walk debugging. Changing code without a hypothesis is the slowest possible approach. Form a guess, test it, then change — the lesson-6 failure mode.
Watch outTesting a whole program instead of a function. If the program is decomposed into functions, test each one. Testing the giant whole hides which piece is wrong.
Check your understanding
What does an assert statement do?
What is print-instrumentation, and when is it useful?
Why does testing work best on well-decomposed functions?
Multiple choice
What does assert do when its condition is false?
Why is an automated test better than eyeballing output?
What is print-instrumentation?
Which is an example of random-walk programming?
What makes a function easy to test?
When debugging, what should you do before changing code?
Why should you test edge cases, not just the normal case?
Where should you leave print-instrumentation after fixing a bug?
Summary and next
Testing is proactive correctness: an assert states an expectation that Python verifies every run, best applied to small, single-job functions. Debugging is hypothesis-driven — form a guess, verify it with print-instrumentation, then change. Together, decomposed code plus tests give you a program you can change without fear.
Next: the mini project. This is where it all comes together — you will write a real, complete program that reads a file, counts words, and reports the result, using the functions, structures and loops you have built across the whole course. It is the capstone.