Testing a class with assert
Creates an object, calls its method, then uses assert to check the result or attribute equals what you expect. If it is False, Python raises an AssertionError.
Understanding Testing a class with assert in ACARA Digital Technologies Years 9–10
What it does: After the invalid call, the assert proves the setter rejected the bad value and kept the old attribute. The test function creates an object, calls only the deposit method, and asserts its effect, so each method gets its own test. assert needs a condition, so use == to compare. A single = is assignment and causes a SyntaxError. You must call the method with brackets. Without them you compare the method itself to a string, which is never equal. A failed assert stops the program with an AssertionError. A passing assert prints nothing. The object must exist and the method must run before there is anything to check.
Common mistakes: A single = tries to assign a value, which is a SyntaxError. An assert must compare with ==, so it should be assert acct.get_balance() == 75. Rex has no quotes, so Python looks for a variable called Rex and raises a NameError. The expected text must be a string: 'Rex'.
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 681–707) and word search (Modules 629–707).
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
assert obj.method() == expectedFill in the Blanks
Spot the Error
Student with a class code? Join your class →