← VCE Software Development U3O1

Testing a function with 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.

Understanding Testing a function with assert in VCE Software Development U3O1

What it does: 50 is the edge between failing and passing, so it is a boundary input. It checks the function is correct right at the limit. Use == to compare the result with the expected value. A single = is for assigning to a variable, so it causes a SyntaxError. A failed assert stops the program with an AssertionError. That is how automatic tests tell you something is wrong. A passing assert is silent. You only see a message when a test fails. A list of test cases in a loop lets you check lots of inputs automatically each time you change the code.

Common mistakes: The parameters must be separated by a comma. It should be def add(a, b):. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 168–177) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly tests that add(2, 3) gives 5?
assert add(2, 3) = 5assert add(2, 3) == 5
Matches
assert add(2, 3) = 5
↓
Uses = (assignment) instead of == (comparison), which causes a SyntaxError
Applying
Meanings
Term
assert
Meaning
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.
Practice
def double(n):
Testing
Gaps
add(2, 3) == 5
Check Answer
Spots
1def add(a b):
2 return a + b

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Warthog' badge — Definition Digger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly tests that add(2, 3) gives 5?
A. assert add(2, 3) = 5
B. assert add(2, 3) == 5 ✓
Use == to compare the result with the expected value. A single = is for assigning to a variable, so it causes a SyntaxError.
2. What happens when an assert condition is False?
A. Python raises an AssertionError ✓
B. Python prints False and carries on quietly
A failed assert stops the program with an AssertionError. That is how automatic tests tell you something is wrong.
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
A passing assert is silent. You only see a message when a test fails.
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 ✓
A list of test cases in a loop lets you check lots of inputs automatically each time you change the code.
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
Good testing covers normal, boundary and invalid inputs. Repeating the same normal input does not find new bugs.

Match the Pairs

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

Key Term

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.

Fill in the Blanks

1. assert add(2, 3) == 5
Hint: Type the keyword that checks a condition and raises an error if it is False
2. assert add(2, 3) == 5
Hint: Type the operator that compares the function result with the expected value
3. cases = [(1, 2, 3), (0, 0, 0)] for a, b, expected in cases: assert add(a, b) == expected
Hint: Type the loop variable that holds the correct result for each test case
4. assert positive(0) == False
Hint: Type the Boolean value a boundary test expects when the input is exactly zero
5. tests = [(2, 3, 5), (0, 0, 0)] for a, b, expected in tests: assert add(a, b) == expected
Hint: Type the keyword that starts a loop to run every test case in the list

Spot the Error

Question 1
A def add(a b):
B return a + b
C assert add(2, 3) == 5
Line A — The parameters must be separated by a comma. It should be def add(a, b):
Question 2
A def double(n):
B return n * 2
C assert double(4) = 8
Line 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
Question 3
A def is_even(n):
B return n % 2 == 0
C assert is_even(4) == False
D print("Test passed")
Line C — 4 is even, so is_even(4) returns True. The expected value is wrong, so assert raises an AssertionError. It should be == True.
Question 4
A def square(n):
B return n + n
C assert square(3) == 9
D assert square(0) == 0
Line B — The function adds instead of multiplying, so square(3) gives 6 and the assert fails. It should be return n * n.
Question 5
A tests = [(-3, 3), (4, 4), (0, 0)]
B for x, want in tests
C assert abs(x) == want
Line B — A for loop header must end with a colon. It should be for x, want in tests:

Student with a class code? Join your class →