← Teacher guide

Test with a table

Students write test cases as comparison lines, find a bug with a wrong-choice test, and fix the code.

Console bridge: text in, text out · Menus and testing · about 35 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 8 min · Investigate 8 min · Modify 8 min · Mix it up 3 min · Make 8 min

Running the lesson

  1. Predict: the common slip is trusting the first two tests. Let students argue before running. Ask the all-True voters what would convince them the function works for every input.
  2. Run: ask which test found the bug and why the first two did not.
  3. Investigate: the last trace line has a wrong expectation. A False test does not always mean the function is wrong: it can be the test.
  4. Modify: students add the missing test, watch it show False, and fix the function.
  5. Make: encourage unusual inputs: a space, an empty answer, a letter.

In class

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.

The hard part (on purpose)

Believing the program works because the easy tests pass. Ask which input has not been tried.

When students get stuck

What you will seeA question to askThe 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.

Answers to the checks

  1. Predict: What do the three test lines show?
    True, True, False. reply("9") returns Please choose 1 or 2 with no full stop, but the test expects one. The first two cases only tried choices that work. The wrong-choice test is the one that found the slip.
  2. Read the tests: Fill in what each test shows.
    True, False, False
    A test shows True only when the function gives back exactly what the test expects. The last line is a wrong expectation, so it shows False even though the function is fine.
  3. Spot the bug: This test was meant to check the wrong-choice reply, but the program stops with an error before it runs. Click the wrong line.
    line 9
    A single = stores a value. A test compares with two: reply("9") == "Please choose 1 or 2.".
  4. Mix it up: You type 1, then 9, then 2. What does the menu show?
    Added $5, Please choose 1 or 2., Pay now
    Each pass prints what reply gives back. 2 shows Pay now, and then break ends the loop.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Add a test

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.")

Modify › Fix the slip

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.")

Make

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 and support

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.

Exit ticket

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.

What each step is for

Where it leads

Next: the challenge. Build a console app that uses every move so far, with test cases of your own.