Summary
In this lesson, you learned that:
- A test is an example that runs:
print(walk_price(45) == 12)printsTruewhen the function returns what you expected andFalsewhen they disagree. Test an ordinary case and both sides of each boundary. - Written before the code, tests tell the AI exactly what you want, and they check its answer as soon as you paste it in. Choose them so that only your rule fits them.
- Tests from an AI need checking too. When a test prints
False, work the case out by hand before you change the function or the test. - Answers often write tests with
assert, which stops the program with anAssertionErrorat the first test that fails. The text after the comma says which test it was.
Practice it with week_total(), which adds up the prices of a week's walks, here without the discount, to keep the tests short. This time, ask for the tests. Asked only for tests of it, answers wrote them with unittest, a Python library you haven't learned, or with assert, so say how you want them written. And check them before you trust them.
Next comes When It Makes Things Up: what to do when an answer uses something that doesn't exist, and how to check what it says about Python.
Instructions
- 1
Attach
script.pyand ask for three tests ofweek_total(), written as you write tests:print()of a comparison with==. Then send it. - 2
Before you trust the tests, work out each expected total by hand, and fix any that is wrong. Then paste the tests at the end of the program and run it: under
Total: 36, it printsTruethree times.