
Code
Test Before You Fix
Found the bug? Before you fix it, write a test that fails because
of it. A test is just a function that checks the code with assert:
def test_countdown_of_one():
assert countdown(1) == [1]
Then follow three steps:
- Red: run the test and watch it fail. This proves it really catches the bug. (An empty test always "passes", so seeing red matters!)
- Fix the code.
- Green: run the test again. Now it passes.
Keep the test forever. If someone brings the bug back later (that's called a regression), the test catches it straight away.
Name tests after the case they check, starting with test_. Tools like
pytest find every test_ function and run them all for you. Here,
we just call them ourselves.
The bug report
average([])crashes. An empty list should give0.grade(90)gives"B", but 90 or more is an"A". (75 or more is a"B"; anything lower is a"C".)
Assignment
- Fill in
test_average_of_empty_list()so it checks thataverage([])is0. - Fill in
test_grade_boundary_90()so it checks thatgrade(90)is"A". - Run it and watch the tests fail. Then fix
averageandgradeuntilAll tests pass!prints.
- Fill in
test_average_of_empty_list()so it checks thataverage([])is0. - Fill in
test_grade_boundary_90()so it checks thatgrade(90)is"A". - Run it and watch the tests fail. Then fix
averageandgradeuntilAll tests pass!prints.
main.py Python sleeping
Press Run to try your code, then Check to test it.