Tests with assert
When you ask an AI for tests, the answer often writes them with assert instead of print():
assert walk_price(45) == 12, "45 minutes"assert checks the condition after it. If it's True, nothing happens and the program goes on. If it's False, the program stops with an AssertionError, and the text after the comma says which test it was. A test that passes prints nothing, so the answer in the editor ends with print("All tests passed"): if the program gets that far, every test before it has passed.
That answer is one someone got: walk_price() with four tests. When you run it, it stops at the second test:
Traceback (most recent call last):
File "<exec>", line 8, in <module>
AssertionError: 45 minutesRead it from the bottom, as any error: the test for 45 minutes failed, on line 8. The tests after it didn't run, so you don't know yet whether they pass. And as with False, the error doesn't say whether the function or the test is wrong.
You don't need to write tests with assert yourself: print() with == does the same job and shows every test, not only the first that fails. But you'll read them in answers.
Instructions
- 1
Run the program. Work out by hand what a walk of 45 minutes costs, and fix what's wrong: the function or the test. Run it again: it prints
All tests passed.