Testing a function with assert

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

Testing a function with assert

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly tests that add(2, 3) gives 5?
A assert add(2, 3) = 5
B assert add(2, 3) == 5
2. What happens when an assert condition is False?
A Python raises an AssertionError
B Python prints False and carries on quietly
3. What does Python do when an assert condition is True?
A Nothing, and the program keeps running
B It prints True on the screen
4. Which is the best way to run many test cases on a function?
A Test one case by hand and assume the rest work
B Put the cases in a list and use a for loop with assert
5. A function should return a student's grade from 0 to 100. Which set of inputs is a good test plan?
A A normal mark like 65, boundaries 0 and 100, and an invalid mark like -5
B Only the mark 65, tried three times

Match the Pairs

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

Left columnRight column
1assert add(2, 3) == 5 AUses = (assignment) instead of == (comparison), which causes a SyntaxError
2for x, want in tests: assert double(x) == want BRuns the same assert check on every test case in a list
3A boundary test checks a value at the edge of the allowed range, such as 0 or 100 CThe expected value is wrong, because add(2, 3) is 5, so the test fails with an AssertionError
4An invalid input test checks the function handles a bad value like a negative number Dassert is_pass(50) == True
5assert add(2, 3) = 5 EChecks that add(2, 3) gives 5 and raises an AssertionError if it does not
6assert add(2, 3) == 6 Fassert grade(-5) == "Invalid"

Definitions

assert

Fill in the Blanks

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

1.
______ add(2, 3) == 5
2.
assert add(2, 3) __ 5
3.
cases = [(1, 2, 3), (0, 0, 0)] for a, b, expected in cases: assert add(a, b) == ________
4.
assert positive(0) == _____
5.
tests = [(2, 3, 5), (0, 0, 0)] ___ a, b, expected in tests: assert add(a, b) == expected

Spot the Error

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

Question 1
A def add(a b):
B return a + b
C assert add(2, 3) == 5
Question 2
A def double(n):
B return n * 2
C assert double(4) = 8
Question 3
A def is_even(n):
B return n % 2 == 0
C assert is_even(4) == False
D print("Test passed")
Question 4
A def square(n):
B return n + n
C assert square(3) == 9
D assert square(0) == 0
Question 5
A tests = [(-3, 3), (4, 4), (0, 0)]
B for x, want in tests
C assert abs(x) == want

Answer Key

Testing a function with assert
Section A: Quick Questions
Q1
B — assert add(2, 3) == 5
Q2
A — Python raises an AssertionError
Q3
A — Nothing, and the program keeps running
Q4
B — Put the cases in a list and use a for loop with assert
Q5
A — A normal mark like 65, boundaries 0 and 100, and an invalid mark like -5
Section B: Match the Pairs
Q1
assert add(2, 3) == 5 → Checks that add(2, 3) gives 5 and raises an AssertionError if it does not
Q2
for x, want in tests: assert double(x) == want → Runs the same assert check on every test case in a list
Q3
A boundary test checks a value at the edge of the allowed range, such as 0 or 100 → assert is_pass(50) == True
Q4
An invalid input test checks the function handles a bad value like a negative number → assert grade(-5) == "Invalid"
Q5
assert add(2, 3) = 5 → Uses = (assignment) instead of == (comparison), which causes a SyntaxError
Q6
assert add(2, 3) == 6 → The expected value is wrong, because add(2, 3) is 5, so the test fails with an AssertionError
Section C: Definitions
Q1
assert — Checks a condition is True, like assert add(2, 3) == 5. If the result is not what you expected, it raises an AssertionError, so test cases can run automatically.
Section D: Fill in the Blanks
Q1
assert add(2, 3) == 5
Q2
assert add(2, 3) == 5
Q3
cases = [(1, 2, 3), (0, 0, 0)] for a, b, expected in cases: assert add(a, b) == expected
Q4
assert positive(0) == False
Q5
tests = [(2, 3, 5), (0, 0, 0)] for a, b, expected in tests: assert add(a, b) == expected
Section E: Spot the Error
Q1
A — The parameters must be separated by a comma. It should be def add(a, b):
Q2
C — A single = assigns a value, so it can't be used to compare. assert needs the comparison operator ==, so it should be assert double(4) == 8
Q3
C — 4 is even, so is_even(4) returns True. The expected value is wrong, so assert raises an AssertionError. It should be == True.
Q4
B — The function adds instead of multiplying, so square(3) gives 6 and the assert fails. It should be return n * n.
Q5
B — A for loop header must end with a colon. It should be for x, want in tests: