Identifying and fixing bugs
Be able to test a program, identify the error, and fix it without restructuring the entire codebase.
Prerequisites
Everyday explanation
A program may work for certain values but crash for others. Therefore, you test with different types of input: a standard case, a small value, a large value, and an unusual case (zero, empty string, negative number).
Once you have identified the error: change as little as possible and run all tests again. If you fix one thing and break another, you will spot it immediately.
Interactive
Test the average function:
def average(lst):
return sum(lst) / len(lst)
average([2, 4]) → 3 ✔. average([5]) → 5 ✔. average([]) → crash (division by zero). The error is only visible with an empty list.
Fix: if len(lst) == 0: return 0. Run all three tests again — now they all pass. Document your tests so you can run them again next time you change the code.
Mastery means
- Test a program with multiple inputs, including edge cases
- Fix an error without introducing new bugs
Sign in to do the exercises and build your mastery up.
Sources
- The Python documentation (PSF licence) — PSF
- CS Unplugged (CC BY-SA 4.0) — CC BY-SA 4.0