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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
assertFill in the Blanks
Spot the Error
Student with a class code? Join your class →