Write test cases (what to type and what the program should reply) as lines that print True or False, and include a wrong choice.
Apps are tested before anyone uses them. A test case says what to type and what should come back, and the computer checks it every time.
A menu is awkward to test by typing: you have to type the same choices every time. If the menu's replies come from a function, you can test the function with one line per case, and the computer says True or False for each.
Make your prediction first. Help opens once you've locked it in.
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.")
Each print line compares what reply gives back with what you expect, and shows True or False. Decide what each line shows.
Run it and compare with your prediction.
| Line | What it does | Why |
|---|---|---|
def reply(choice): |
The menu's replies, as a function | A function can be called with any choice, with no typing |
reply("9") == "Please choose 1 or 2." |
One test case: given 9, expect this reply | == gives True when they match and False when they do not |
print(...) |
Shows the result | Run all the tests at once and read down the Trues and Falses |
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.")
Each line is one test case.
A test that cannot run.
Fill in what each test shows.
| test | shows |
|---|---|
| reply("1") == "Added $5" | True |
| reply("2") == "Pay now" | |
| reply("9") == "Please choose 1 or 2." | |
| reply("1") == "Added $4" |
This test was meant to check the wrong-choice reply, but the program stops with an error before it runs. Click the wrong line.
Add a test line for a wrong choice.
Copy a test line and change what you type, and what you expect, to a wrong choice such as 9.
If it shows False, fix the function, and run the tests again.
The new test shows False. Compare the reply text in the function with the text in the test.
Add a test for the wrong choice 9.
Run your program. This ticks when your code does all of the above.
Fix the function so all the tests show True.
Run your program. This ticks when your code does all of the above.
This mixes the menu loop and a function that gives back an answer.
def reply(choice):
if choice == "1":
return "Added $5"
elif choice == "2":
return "Pay now"
else:
return "Please choose 1 or 2."
while True:
choice = input("1 add $5, 2 pay: ")
print(reply(choice))
if choice == "2":
break
Here is a till function. Write three test lines for it: what to type and what it should reply. Include a wrong choice. Run the tests and fix anything that shows False.
One test per line: print(reply("typed") == "expected").
Include a wrong choice such as 9, and a choice with a space, such as " 1".
Write at least three test lines for the till, including a wrong choice.
Run your program. This ticks when your code does all of the above.
Students write test cases as comparison lines, find a bug with a wrong-choice test, and fix the code. About 35 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Believing the program works because the easy tests pass. Ask which input has not been tried.
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.
Next: the challenge. Build a console app that uses every move so far, with test cases of your own.