← Console bridge: text in, text out

Test with a table

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.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
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.

What do the three test lines show?

RunRun it and compare the result with your prediction.

Run

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
lesson.py
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.")
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
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.

testshows
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.

ModifyChange the code on purpose and check the result.

Modify

Add a test line for a wrong choice.

Need a hint?

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.

Need a hint?

The new test shows False. Compare the reply text in the function with the text in the test.

Try it yourselfYour edits are saved in this browser.

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.

      InvestigateTrace how each line of code connects to what you see.

      Mix it up

      This mixes the menu loop and a function that gives back an answer.

      lesson.py
      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
      
      You type 1, then 9, then 2. What does the menu show?

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Make

      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.

      Try it yourselfYour edits are saved in this browser.
      Need a hint?

      One test per line: print(reply("typed") == "expected").

      Another hint

      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.

        For teachers

        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.

        Students will be able to

        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.

        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

        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.

        Where it leads

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

        What each step is for

        Now you can
        Next lesson11. Challenge: a console appRead a working café till, change one thing and predict the result, then make a console app of your own with a menu, a checked number, a total and tests.