Testing one module in isolation

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

Testing one module in isolation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which approach tests a function in isolation?
A Call the function on its own with fixed inputs and compare the result to the expected output
B Run the whole program and hope the function gets used
2. Why do we test one module on its own before joining it to the rest of the solution?
A It lets us skip testing once the modules are joined
B A failing test points to that module, so the fault is easy to find
3. A function calculate_gst(price) is tested with price = 100 and the expected output is 10. What goes in the test table?
A Input: calculate_gst, Expected: 100, Actual: 10
B Input: 100, Expected: 10, Actual: the value the function returns
4. Which test is an isolated test of a class method?
A Create one Student object, call its get_grade() method and check the result
B Start the whole GUI, enter data and click through every screen
5. A test of a function in isolation fails. Where is the fault most likely to be?
A Inside that function
B In another module that has not been written yet

Match the Pairs

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

Left columnRight column
1assert calc_gst(100) == 10 ATests the calc_gst() function alone with a fixed input of 100 and checks it returns the expected 10
2acct.deposit(50) assert acct.balance == 150 BThis runs the whole program through main() instead of calling the one function under test, so a failure could come from any module.
3Test a function by itself using a fixed input and a known expected output before joining it to the main program Cassert is_adult(18) == True
4After fixing a faulty function, run the same test again to confirm the fix works and has not broken anything Dassert average([2, 4, 6]) == 4
5result = main() assert result == 25 ETests one class method on its own by calling it, then checking the object's state matches the expected value
6def total(a, b): return a - b assert total(2, 3) == 5 # fixed, so no need to re-test FThe code was changed but the test was not run again, so there is no evidence the fix works or that it has not caused new errors.

Definitions

unit testing

Fill in the Blanks

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

1.
def double(n): return n * 2 assert ______(4) == 8
2.
def area(w, h): return w * h assert area(3, 4) __ 12
3.
acc = Account(100) acc.________(30) assert acc.balance == 70
4.
def adult(age): return age >= 18 assert adult(18) == ____
5.
def cube(n): return n ** 3 ______ cube(2) == 8

Spot the Error

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

Question 1
A def double(n):
B return n * 2
C assert double(4) = 8
Question 2
A def is_adult(age):
B return age > 18
C assert is_adult(18) == True
D assert is_adult(17) == False
Question 3
A def add(a, b)
B return a + b
C assert add(2, 3) == 5
Question 4
A c = Counter
B c.add()
C assert c.count == 1
D print('Counter test passed')
Question 5
A def total(price, qty):
B return price * qty
C assert total(5, 3) == 8

Answer Key

Testing one module in isolation
Section A: Quick Questions
Q1
A — Call the function on its own with fixed inputs and compare the result to the expected output
Q2
B — A failing test points to that module, so the fault is easy to find
Q3
B — Input: 100, Expected: 10, Actual: the value the function returns
Q4
A — Create one Student object, call its get_grade() method and check the result
Q5
A — Inside that function
Section B: Match the Pairs
Q1
assert calc_gst(100) == 10 → Tests the calc_gst() function alone with a fixed input of 100 and checks it returns the expected 10
Q2
acct.deposit(50) assert acct.balance == 150 → Tests one class method on its own by calling it, then checking the object's state matches the expected value
Q3
Test a function by itself using a fixed input and a known expected output before joining it to the main program → assert is_adult(18) == True
Q4
After fixing a faulty function, run the same test again to confirm the fix works and has not broken anything → assert average([2, 4, 6]) == 4
Q5
result = main() assert result == 25 → This runs the whole program through main() instead of calling the one function under test, so a failure could come from any module.
Q6
def total(a, b): return a - b assert total(2, 3) == 5 # fixed, so no need to re-test → The code was changed but the test was not run again, so there is no evidence the fix works or that it has not caused new errors.
Section C: Definitions
Q1
unit testing — Testing one function or class on its own, with its own test table, before joining it to the rest of the program, so a failing test points to that module.
Section D: Fill in the Blanks
Q1
def double(n): return n * 2 assert double(4) == 8
Q2
def area(w, h): return w * h assert area(3, 4) == 12
Q3
acc = Account(100) acc.withdraw(30) assert acc.balance == 70
Q4
def adult(age): return age >= 18 assert adult(18) == True
Q5
def cube(n): return n ** 3 assert cube(2) == 8
Section E: Spot the Error
Q1
C — A test compares the actual result with the expected result, so it needs the comparison operator ==. A single = is assignment and causes a syntax error. It should be assert double(4) == 8.
Q2
B — The boundary test is_adult(18) expects True, but age > 18 returns False for 18. The failing test points straight to this function. It should be return age >= 18.
Q3
A — The function header is missing its colon, so the module cannot be loaded and tested. It should be def add(a, b):.
Q4
A — Without brackets, c refers to the class itself and no object is created, so c.add() cannot test the method. It should be c = Counter() to create an instance.
Q5
C — The expected value in a test table must come from the requirement, not from a guess. 5 * 3 should give 15, so this test would fail even though the function is correct. It should be == 15.