Course 3.2Lesson 1 of 5
A test runs your code with an input and compares the answer with one that you worked out from the rules, not from the code. assert says that something must be True, and stops with an AssertionError and an optional message when it is not, which is useful for checking input and for writing quick tests. A table of tests holds the inputs and the expected answers, and a loop runs them all and counts the failures, so one run shows all the problems. Choose the cases with care: one in each branch, both sides of every edge, the smallest and the biggest input, and the empty case. A failing test names the input, the expected answer and the answer that was given, and tests let you change code without fear.
Try the code and the simulations beside the lesson.
After this lesson you can
assert to check input and to write a quick test,You wrote a function. You tried it with an input or two, and the answers looked right. Is it correct? Not necessarily. The bugs hide in the inputs that you did not try: the empty list, the edge of a range, the number that is exactly 75. A test is how you find out. It runs the code on an input, and compares the answer with one that you worked out in advance, from the rules, never from the code.
Why not from the code? If you copy the answer that the code gives, then a bug is copied into the test, and the test says "all is well". The expected answer must come from your understanding of what the function should do.
Python has a statement made for checks: assert. It says: "this must be True". If it is not, the program stops with an AssertionError. You can add a message after a comma.
Predict first. Which of the four lines produce a message? Then run it.
assert average([2, 4, 6]) == 4 is a test. It passes, so nothing is shown. A passing assert is silent.assert len(numbers) > 0, "average of an empty list" inside the function is a check on the input. If somebody calls average([]), the function stops with a clear message. (For input from a user, use a ValueError as you learned. An assert is for mistakes of the programmer, not of the user.)average([1, 2]) is 1.5, not 2. We caught the AssertionError to show the message. In your tests, you would let it stop the program, so that you see it.What happens when an assert fails and nobody catches it?
One caution: Python can be started with a flag that switches asserts off, so they are for testing, never for the real work of a program, such as validating a password.
A pile of asserts stops at the first failure. You would like to see all the failures at once. For this, use a table of tests: a list of cases, and a loop.
Choose cases that would catch the likely bugs: zero, a negative number, the edges, and an ordinary case.
You cannot test every input. A good set is small and catches most bugs.
| Kind of case | Example for the grade | Why |
|---|---|---|
| one in each branch | 95, 80, 60, 20 | each branch runs at least once |
| both sides of every edge | 89, 90 and 74, 75 | the bugs between > and >= live here |
| the smallest and the biggest input | 0, 100 | the ends of the range |
| the empty or the zero case | [], 0, "" | many functions forget it |
| a strange case | a negative number, repeats | what the programmer did not expect |
Eight to twelve cases usually catch most bugs.
assert for user input. Asserts can be switched off. Use ValueError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
assert condition, "message" stops with an AssertionError when the condition is False. It is silent when True.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
assert show?mark > 75 instead of mark >= 75?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.