Looping through test cases and printing PASS or FAIL

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

Looping through test cases and printing PASS or FAIL

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop header unpacks each (input, expected) tuple in a list called tests?
A for value, expected in tests:
B for value in expected, tests:
2. Inside the loop, how do you get the actual result of the function double() for the test input?
A actual = double(expected)
B actual = double(value)
3. Which line checks whether a test passed?
A if actual == expected:
B if actual = expected:
4. A test loop prints PASS when actual equals expected. When is FAIL printed?
A Every time the loop starts a new test
B In the else branch, when actual does not equal expected
5. Which FAIL message is most useful for finding a bug?
A print('FAIL', value, 'expected', expected, 'got', actual)
B print('FAIL')

Match the Pairs

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

Left columnRight column
1for value, expected in tests: AA single = was used in the if condition. It assigns a value instead of comparing, which causes a SyntaxError. It should be ==.
2actual = double(value) BThe loop only has one variable, so the whole (input, expected) tuple goes into value and expected is never defined. It should be for value, expected in tests.
3Print PASS when the actual result matches the expected result Cif actual == expected: print("PASS")
4On a failure, print both values so you can see what went wrong DLoops through the list of test tuples and unpacks each one into an input value and an expected result
5for value, expected in tests: actual = double(value) if actual = expected: print("PASS") ECalls the function with the test input and stores the real result to compare with the expected result
6for value in tests: actual = double(value) if actual == expected: print("PASS") Felse: print("FAIL", expected, actual)

Definitions

for value, expected in tests:

Fill in the Blanks

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

1.
for value, expected __ tests: actual = double(value)
2.
for value, ________ in tests: actual = double(value)
3.
for value, expected in tests: actual = double(_____)
4.
for value, expected in tests: actual = double(value) if actual __ expected:
5.
print("PASS") ____: print("FAIL", actual, expected)

Spot the Error

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

Question 1
A tests = [(2, 4), (3, 9)]
B for value, expected in tests:
C actual = square(value)
D if actual = expected:
E print("PASS")
Question 2
A for value, expected in tests:
B actual = double(value)
C if actual == expected
D print("PASS")
E else:
F print("FAIL", actual)
Question 3
A for value in tests:
B actual = square(value)
C if actual == expected:
D print("PASS")
Question 4
A tests = [(2, 3), (7, 8)]
B for value, expected in tests:
C actual = add_one(value)
D if actual == value:
E print("PASS")
Question 5
A for value, expected in tests:
B actual = triple(expected)
C if actual != expected:
D print("FAIL", value)
E else:
F print("PASS")

Answer Key

Looping through test cases and printing PASS or FAIL
Section A: Quick Questions
Q1
A — for value, expected in tests:
Q2
B — actual = double(value)
Q3
A — if actual == expected:
Q4
B — In the else branch, when actual does not equal expected
Q5
A — print('FAIL', value, 'expected', expected, 'got', actual)
Section B: Match the Pairs
Q1
for value, expected in tests: → Loops through the list of test tuples and unpacks each one into an input value and an expected result
Q2
actual = double(value) → Calls the function with the test input and stores the real result to compare with the expected result
Q3
Print PASS when the actual result matches the expected result → if actual == expected: print("PASS")
Q4
On a failure, print both values so you can see what went wrong → else: print("FAIL", expected, actual)
Q5
for value, expected in tests: actual = double(value) if actual = expected: print("PASS") → A single = was used in the if condition. It assigns a value instead of comparing, which causes a SyntaxError. It should be ==.
Q6
for value in tests: actual = double(value) if actual == expected: print("PASS") → The loop only has one variable, so the whole (input, expected) tuple goes into value and expected is never defined. It should be for value, expected in tests.
Section C: Definitions
Q1
for value, expected in tests: — Loops over each (input, expected) tuple in tests, unpacking it into two variables. Then you call the function, compare actual to expected, and print PASS, or FAIL with both values.
Section D: Fill in the Blanks
Q1
for value, expected in tests: actual = double(value)
Q2
for value, expected in tests: actual = double(value)
Q3
for value, expected in tests: actual = double(value)
Q4
for value, expected in tests: actual = double(value) if actual == expected:
Q5
print("PASS") else: print("FAIL", actual, expected)
Section E: Spot the Error
Q1
D — A single = assigns a value, so it cannot compare actual with expected. The test needs the comparison operator ==: if actual == expected:
Q2
C — An if statement must end with a colon. The line should be: if actual == expected:
Q3
A — Each test is a tuple of (input, expected). The loop must unpack both parts: for value, expected in tests: Otherwise expected is never set inside the loop.
Q4
D — The actual result must be compared with the expected result, not the input value. It should be: if actual == expected:
Q5
B — The function must be called with the test input, value, to get the actual result. Passing expected would not test the function properly. It should be: actual = triple(value)