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:

  1. Red: run the test and watch it fail. This proves it really catches the bug. (An empty test always "passes", so seeing red matters!)
  2. Fix the code.
  3. 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 give 0.
  • grade(90) gives "B", but 90 or more is an "A". (75 or more is a "B"; anything lower is a "C".)

Assignment

  1. Fill in test_average_of_empty_list() so it checks that average([]) is 0.
  2. Fill in test_grade_boundary_90() so it checks that grade(90) is "A".
  3. Run it and watch the tests fail. Then fix average and grade until All tests pass! prints.