← ACARA Digital Technologies Years 9–10

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'.

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 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.

Learning
Duo
Account starts with balance 100. After acc.deposit(50), which line correctly checks the attribute?
assert acc.balance = 150assert acc.balance == 150
Matches
assert car.drive(10) = 10
↓
A single = assigns a value and is a syntax error in an assert. Use == to compare the returned value with the expected one.
Applying
Meanings
Term
assert obj.method() == expected
Meaning
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.
Practice
class Score:
Testing
Gaps
acc.balance == 70
Check Answer
Spots
3assert acct.balance == 75
4assert acct.get_balance() = 75

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

The 'Blue-Ringed Octopus' badge — Reef Legend, 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. Account starts with balance 100. After acc.deposit(50), which line correctly checks the attribute?
A. assert acc.balance = 150
B. assert acc.balance == 150 ✓
assert needs a condition, so use == to compare. A single = is assignment and causes a SyntaxError.
2. Which assert checks the value RETURNED by a method?
A. assert dog.speak() == "Woof" ✓
B. assert dog.speak == "Woof"
You must call the method with brackets. Without them you compare the method itself to a string, which is never equal.
3. What happens when the condition in an assert is False?
A. Python quietly carries on to the next line
B. Python raises an AssertionError ✓
A failed assert stops the program with an AssertionError. A passing assert prints nothing.
4. Which is the right order for a simple class test?
A. Create the object, call the method, then assert ✓
B. Assert first, then create the object and call the method
The object must exist and the method must run before there is anything to check.
5. A setter ignores negative values. Pet starts with age 3, then you call pet.set_age(-5). Which test is correct?
A. assert pet.age == 3 ✓
B. assert pet.age == -5
Invalid input should be rejected, so the old value stays. The test proves the setter protects the attribute.

Match the Pairs

assert account.balance == 150 → Checks that the balance attribute equals 150 after a method has run, and raises an AssertionError if it does not
assert dog.speak() == "Woof" → Calls the speak() method and checks that the value it returns is exactly "Woof"
Test a setter with invalid input: call it with a bad value, then assert the attribute did not change → player.set_age(-5) assert player.age == 14
Write one test per method: create an object, call that method, then assert on the result → def test_deposit(): acc = Account(100) acc.deposit(50) assert acc.balance == 150
assert car.drive(10) = 10 → A single = assigns a value and is a syntax error in an assert. Use == to compare the returned value with the expected one.
car.speed == 20 assert car.speed == 20 → The method call car.accelerate() is missing, so the speed never changes and the assert fails. Call the method before checking the attribute.

Key Term

assert obj.method() == expected
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.

Fill in the Blanks

1. acc = Account(50) acc.deposit(20) assert acc.balance == 70
Hint: Type the keyword that checks a condition and raises an AssertionError if it is False.
2. acc = Account(50) acc.deposit(20) assert acc.balance == 70
Hint: Type the name of the method being called on the object. It adds money to the balance.
3. acc = Account(50) acc.withdraw(20) assert acc.balance == 30
Hint: Type the balance you expect after taking 20 out of the starting amount.
4. acc = Account(50) acc.set_balance(-10) assert acc.balance == 50
Hint: Type the name of the setter method being tested with an invalid negative value.
5. acc = Account(50) acc.deposit(20) assert acc.balance == 70
Hint: Type the attribute name that holds the money, checked after the method call.

Spot the Error

Question 1
A acct = Account(50)
B acct.deposit(25)
C assert acct.balance == 75
D assert acct.get_balance() = 75
Line D — 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.
Question 2
A dog = Dog('Rex')
B assert dog.name == Rex
C dog.rename('Max')
D assert dog.name == 'Max'
Line B — Rex has no quotes, so Python looks for a variable called Rex and raises a NameError. The expected text must be a string: 'Rex'.
Question 3
A lamp = Lamp()
B lamp.switch_on()
C assert lamp.is_on() == True
Line C — is_on is an attribute, not a method. Adding () tries to call a True/False value and causes a TypeError. It should be assert lamp.is_on == True.
Question 4
A p = Player('Ana')
B p.set_health = -5
C assert p.health == 100
Line B — This overwrites the set_health method with -5 instead of calling it, so the setter never gets the invalid input. It should be p.set_health(-5).
Question 5
A car = car('red', 0)
B car.drive(10)
C assert car.distance == 10
Line A — The class is named Car with a capital C. Python is case-sensitive, so car is not defined and raises a NameError. It should be car = Car('red', 0).

Student with a class code? Join your class →