Students write test cases as comparison lines, find a bug with a wrong-choice test, and fix the code.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 8 min · Investigate 8 min · Modify 8 min · Mix it up 3 min · Make 8 min
On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.
Swap functions with a partner. Write a test that you think will find a bug in theirs.
Believing the program works because the easy tests pass. Ask which input has not been tried.
| What you will see | A question to ask | The fix |
|---|---|---|
| Every test shows False. | Are the expected texts exactly the same as the replies, including spaces and full stops? | Copy the reply text from the function into the test. |
| A SyntaxError on a test line. | Is it = or == in the comparison? | Tests compare with two equals signs. |
One possible solution for each task. Other correct programs are fine.
def reply(choice):
if choice == "1":
return "Added $5"
elif choice == "2":
return "Pay now"
else:
return "Please choose 1 or 2"
print(reply("1") == "Added $5")
print(reply("2") == "Pay now")
print(reply("9") == "Please choose 1 or 2.")
def reply(choice):
if choice == "1":
return "Added $5"
elif choice == "2":
return "Pay now"
else:
return "Please choose 1 or 2."
print(reply("1") == "Added $5")
print(reply("2") == "Pay now")
print(reply("9") == "Please choose 1 or 2.")
def reply(choice):
if choice == "1":
return "Added $5"
elif choice == "2":
return "Pay now"
else:
return "Please choose 1 or 2."
print(reply("1") == "Added $5")
print(reply("2") == "Pay now")
print(reply("9") == "Please choose 1 or 2.")
print(reply(" 1") == "Please choose 1 or 2.")
Stretch: Add a test for the choice 2 with the number typed as 2 instead of "2". What happens?
Support: Give the test lines with the expected text missing and ask for it.
Why should a test list always include a wrong input?
Wrong inputs are where bugs hide. If only the cases that work are tested, a bug in the other cases is never found.
Next: the challenge. Build a console app that uses every move so far, with test cases of your own.