Testing a class with assert

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

Testing a class with assert

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which assert checks the value RETURNED by a method?
A assert dog.speak() == "Woof"
B assert dog.speak == "Woof"
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
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
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

Match the Pairs

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

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

Definitions

assert obj.method() == expected

Fill in the Blanks

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

1.
acc = Account(50) acc.deposit(20) ______ acc.balance == 70
2.
acc = Account(50) acc._______(20) assert acc.balance == 70
3.
acc = Account(50) acc.withdraw(20) assert acc.balance == __
4.
acc = Account(50) acc.___________(-10) assert acc.balance == 50
5.
acc = Account(50) acc.deposit(20) assert acc._______ == 70

Spot the Error

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

Question 1
A acct = Account(50)
B acct.deposit(25)
C assert acct.balance == 75
D 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'
Question 3
A lamp = Lamp()
B lamp.switch_on()
C assert lamp.is_on() == True
Question 4
A p = Player('Ana')
B p.set_health = -5
C assert p.health == 100
Question 5
A car = car('red', 0)
B car.drive(10)
C assert car.distance == 10

Answer Key

Testing a class with assert
Section A: Quick Questions
Q1
B — assert acc.balance == 150
Q2
A — assert dog.speak() == "Woof"
Q3
B — Python raises an AssertionError
Q4
A — Create the object, call the method, then assert
Q5
A — assert pet.age == 3
Section B: Match the Pairs
Q1
assert account.balance == 150 → Checks that the balance attribute equals 150 after a method has run, and raises an AssertionError if it does not
Q2
assert dog.speak() == "Woof" → Calls the speak() method and checks that the value it returns is exactly "Woof"
Q3
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
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
acc = Account(50) acc.deposit(20) assert acc.balance == 70
Q2
acc = Account(50) acc.deposit(20) assert acc.balance == 70
Q3
acc = Account(50) acc.withdraw(20) assert acc.balance == 30
Q4
acc = Account(50) acc.set_balance(-10) assert acc.balance == 50
Q5
acc = Account(50) acc.deposit(20) assert acc.balance == 70
Section E: Spot the Error
Q1
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.
Q2
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'.
Q3
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.
Q4
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).
Q5
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).