Write Tests That Catch the Bug
Intermediate · Write the code · from Program Design and Testing: Plan, Build and Debug Code
From the lesson: Program Design and Testing: Plan, Build and Debug Code
Every app on your phone began as an idea that people turned into a working program by planning it, building it in small steps, and testing it until it did what its users needed. That app is a computing innovation: something new that depends on a program to do its job.
Read the lessonThe drama club's requirements: under 12 pays $5, ages 12 to 64 pay $10, and 65 and over pay $6.
Write price_tests() so it returns a list of test cases, where each test case is a tuple (age, expected_price). The checker runs your tests against a correct ticket_price and against buggy versions that each have one boundary in the wrong place. Your tests pass when:
1. you have at least 4 test cases and every expected price matches the requirements, and
2. every buggy version fails at least one of your tests.
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| def correct(age): if age < 12: return 5 if age < 65: return 10 return 6 tests = price_tests() print(len(tests) >= 4 and all(correct(age) == price for age, price in tests)) | True |
| def buggy(age): if age < 12: return 5 if age <= 65: return 10 return 6 caught = any(buggy(age) != price for age, price in price_tests()) print("caught" if caught else "missed") | caught |
More Program Design exercises
- Handle Volume Button EventsBeginner
- Fix the Off-by-One BugBeginner