Testing a Python program with test cases

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing a Python program with test cases

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A test table lists the input 15 and the expected output 'Pass'. What should you do with it?
A Change 'Pass' in the table to match whatever the program prints
B Run the program with 15 and compare the real output to 'Pass'
2. Your program prints 'Fail' but the test table expected 'Pass'. What do you record?
A Fail, because the real output does not match the expected result
B Pass, because the program ran without crashing
3. Your program has: if score >= 50: print('Pass') else: print('Fail'). Which set of inputs tests every branch?
A 80 and 20
B 80 and 90
4. A program has if, elif and else. How many test cases do you need to test every branch at least once?
A 1 (one test covers the whole program)
B 3 (one for each branch)
5. The rule is: if age >= 13 then print('Allowed'). Which is a boundary value test?
A Testing age 13 (and 12)
B Testing age 100

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Test 1: input 15, expected 'Pass' ATests: input 80 -> 'Pass', input 20 -> 'Fail'
2Test 2: input 50, expected 'Pass', actual 'Fail' -> FAIL BThe real output did not match the expected result, so you record a fail and then look for the bug
3Test every branch: choose one input that runs the if part and one that runs the else part CNo expected result was written. Without it you cannot compare the real output and decide pass or fail.
4Boundary values: test the numbers right on the edge of a condition, such as just below, on and just above 50 DOnly one branch is tested. You also need an input such as 10 for the else branch, plus boundary values like 49 and 50.
5Test table: input 80, expected output: (blank) ETests: input 49, input 50, input 51
6Only test: input 90 for if score >= 50 / else FA test case: one chosen input paired with the output you expect, written in your test table before you run the program

Definitions

test case

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
expected = "Pass" result = grade(50) print(result == ________)
2.
# Pass mark is 50, so test the boundary result = grade(__) print(result)
3.
if result __ expected: print("PASS")
4.
tests = [49, 50, 51] for t in _____: print(t, grade(t))
5.
result = grade(49) if result __ "Fail": print("FAIL: test did not match")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A result = grade(50)
B expected = 'Pass'
C if result = expected:
D print('PASS')
Question 2
A mark = 49
B if mark >= 50
C print('Pass')
D else:
E print('Fail')
Question 3
A expected = 'Fail'
B actual = grade(49)
C print('Result:' actual)
D print('Expected:', expected)
Question 4
A for mark in [0, 49, 50, 100]:
B result = grade(mark)
C print(mark, result)
D print('Tests done'
Question 5
A tests = [0, 49, 50, 100]
B for t in test:
C print(t, grade(t))

Answer Key

Testing a Python program with test cases
Section A: Quick Questions
Q1
B — Run the program with 15 and compare the real output to 'Pass'
Q2
A — Fail, because the real output does not match the expected result
Q3
A — 80 and 20
Q4
B — 3 (one for each branch)
Q5
A — Testing age 13 (and 12)
Section B: Match the Pairs
Q1
Test 1: input 15, expected 'Pass' → A test case: one chosen input paired with the output you expect, written in your test table before you run the program
Q2
Test 2: input 50, expected 'Pass', actual 'Fail' -> FAIL → The real output did not match the expected result, so you record a fail and then look for the bug
Q3
Test every branch: choose one input that runs the if part and one that runs the else part → Tests: input 80 -> 'Pass', input 20 -> 'Fail'
Q4
Boundary values: test the numbers right on the edge of a condition, such as just below, on and just above 50 → Tests: input 49, input 50, input 51
Q5
Test table: input 80, expected output: (blank) → No expected result was written. Without it you cannot compare the real output and decide pass or fail.
Q6
Only test: input 90 for if score >= 50 / else → Only one branch is tested. You also need an input such as 10 for the else branch, plus boundary values like 49 and 50.
Section C: Definitions
Q1
test case — A chosen input paired with the output you expect. Run the program with it, compare the real output to the expected result in your test table, then record pass or fail.
Section D: Fill in the Blanks
Q1
expected = "Pass" result = grade(50) print(result == expected)
Q2
# Pass mark is 50, so test the boundary result = grade(50) print(result)
Q3
if result == expected: print("PASS")
Q4
tests = [49, 50, 51] for t in tests: print(t, grade(t))
Q5
result = grade(49) if result != "Fail": print("FAIL: test did not match")
Section E: Spot the Error
Q1
C — A single = assigns a value. To compare the real result with the expected result you need ==, so it should be: if result == expected:
Q2
B — An if line must end with a colon. It should be: if mark >= 50:
Q3
C — The items inside print() must be separated by a comma. It should be: print('Result:', actual)
Q4
D — The closing bracket is missing, so Python reports a syntax error. It should be: print('Tests done')
Q5
B — The list is called tests, but the loop uses test, which doesn't exist and causes a NameError. It should be: for t in tests: