
Guard the Boundaries
A test is code that runs your code and checks the result. The
simplest test is a function full of assert statements:
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
If every assert holds, nothing happens. If one fails, Python raises
AssertionError and you know something broke. On a real machine,
tools like pytest find every test_* function and run them for you.
Good tests hunt bugs
A test that only checks easy cases proves very little. Bugs love to
hide at boundaries, the exact places where behavior changes:
> vs >=, the first/last item, zero, empty input.
For "4 hours or more is steady", test 3 (just below) and 4 (exactly on the line).
Pip tip: To judge your tests, imagine someone sneaks a tiny bug into the code. Would your test notice? This idea is called mutation testing, and it's exactly how this lesson is checked!
Assignment
glow_grade(hours) returns "dim" below 4, "steady" from 4 to 7,
and "radiant" from 8 up. It's correct. Write test_glow_grade() so
that it passes on this code but fails if someone breaks
either boundary (4 or 8). Then call it and print All checks passed!.
glow_grade(hours) returns "dim" below 4, "steady" from 4 to 7,
and "radiant" from 8 up. It's correct. Write test_glow_grade() so
that it passes on this code but fails if someone breaks
either boundary (4 or 8). Then call it and print All checks passed!.
Press Run to try your code, then Check to test it.