← ACARA Digital Technologies Years 9–10

Test coverage of branches and loops

Testing so that every if/elif/else branch runs at least once and every loop is tried running zero times, once and many times. Any code never run is untested.

Understanding Test coverage of branches and loops in ACARA Digital Technologies Years 9–10

What it does: This code has three branches (if, elif, else). Test scores such as 90, 60 and 20 make each branch run once, giving full branch coverage. Looping over an empty list runs the loop body zero times. This is one of the three loop cases that must be tested, and the print line is never run.

Common mistakes: An if statement must end with a colon, so this line should be 'if n > 0:'. Once it is fixed, full branch coverage needs tests such as 5, -3 and 0 so every branch runs. A total must start at 0. With an empty list the loop runs zero times, so the program would print 1 instead of 0. It should be 'total = 0'. This is why testing the zero-times case matters.

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 536–590) and word search (Modules 536–622).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A program has an if, an elif and an else. Which set of tests gives full branch coverage?
At least one test that runs the if, one that runs the elif and one that runs the elseThree tests that all run the if branch with different numbers
Matches
# Tests run: age = 20 only if age >= 18: print("Adult") else: print("Minor")
↓
Only age 20 was tested, so the else branch never ran. The Minor line is untested code. Add a test such as age 10.
Applying
Meanings
Term
test coverage
Meaning
Testing so that every if/elif/else branch runs at least once and every loop is tried running zero times, once and many times. Any code never run is untested.
Practice
def total(nums):
Testing
Gaps
:
Check Answer
Spots
1def sign(n):
2 if n > 0

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

The 'Flamingo' badge — Balanced Coder, 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. A program has an if, an elif and an else. Which set of tests gives full branch coverage?
A. At least one test that runs the if, one that runs the elif and one that runs the else ✓
B. Three tests that all run the if branch with different numbers
Branch coverage means every branch runs at least once. Repeating the same branch does not test the elif or the else.
2. Code: if mark >= 50: print('Pass') else: print('Fail'). A student only tests mark = 80. What is untested?
A. The else branch ✓
B. The if branch
A mark of 80 makes the condition true, so only the if branch runs. The else branch is untested. Try a mark such as 30 as well.
3. Which loop test checks that a for loop copes with an empty list?
A. Running it many times
B. Running it zero times ✓
An empty list means the loop body never runs. Testing zero iterations checks the program still works when there is nothing to loop over.
4. Full loop coverage means testing a loop running...
A. only the maximum number of times
B. zero times, once and many times ✓
Zero, one and many iterations each can reveal different bugs, such as off-by-one errors or code that assumes the loop always runs.
5. After testing, the coverage check shows one line inside an elif was never run. What does this tell you?
A. That line is untested and could hide a bug ✓
B. That line must be correct because it never caused an error
Code that never runs has never been checked. No error only means nothing went wrong in the parts that did run.

Match the Pairs

if mark >= 50: print("Pass") else: print("Fail") → Needs at least two tests for full branch coverage: one mark that is 50 or more and one that is below 50.
for item in cart: total += item → Test the loop with an empty list (zero times), a one-item list (once) and a longer list (many times).
Every branch of an if/elif/else needs its own test value, so three branches need at least three tests. → if score >= 80: grade = "A" elif score >= 50: grade = "B" else: grade = "C"
A loop that never runs when given an empty list shows the zero-times case, and any code inside it stays untested. → for n in []: print(n)
# Tests run: age = 20 only if age >= 18: print("Adult") else: print("Minor") → Only age 20 was tested, so the else branch never ran. The Minor line is untested code. Add a test such as age 10.
# Tests run: count = 5 only while count > 0: count -= 1 → The loop was only tested running many times. It was never tested running zero times (count = 0) or once (count = 1), so edge bugs could be missed.

Key Term

test coverage
Testing so that every if/elif/else branch runs at least once and every loop is tried running zero times, once and many times. Any code never run is untested.

Fill in the Blanks

1. if mark >= 50: print("Pass") else:
Hint: Type the keyword for the branch that only runs when the condition is false, so it needs a test with a low mark.
2. items = [] for x in items: print(x)
Hint: Type the keyword that starts this loop. The empty list means the loop is tested running zero times.
3. count = 0 for i in range(1): count += 1
Hint: Type the number that makes this loop run exactly one time.
4. for n in [2, 4, 6]: total += n print(total)
Hint: Type the operator that adds each value to the running total on every pass of the loop, which is tested with many items.
5. if age < 13: print("Child") elif age < 20:
Hint: Type the keyword for a second condition that is only checked when the first is false. This branch stays untested unless a test reaches it.

Spot the Error

Question 1
A def sign(n):
B if n > 0
C return 'pos'
D elif n < 0:
E return 'neg'
F return 'zero'
Line B — An if statement must end with a colon, so this line should be 'if n > 0:'. Once it is fixed, full branch coverage needs tests such as 5, -3 and 0 so every branch runs.
Question 2
A items = []
B total = 1
C for x in items:
D total += x
E print(total)
Line B — A total must start at 0. With an empty list the loop runs zero times, so the program would print 1 instead of 0. It should be 'total = 0'. This is why testing the zero-times case matters.
Question 3
A def count_pos(nums):
B c = 0
C for n in nums:
D if n < 0:
E c += 1
F return c
Line D — The function should count positive numbers, so the condition must be 'n > 0'. Tests with [], [4] and [3, -1, 5] (loop zero, once and many times) would show the wrong counts.
Question 4
A def total(nums):
B t = 0
C for n in nums:
D t = n
E return t
F print(total([]))
Line D — 't = n' replaces the total each time instead of adding to it. It should be 't += n'. A test with one item would still pass, so only a loop-many-times test such as [2, 3, 4] reveals the bug.
Question 5
A mark = 75
B if mark => 80:
C print('High')
D else:
E print('Low')
Line B — '=>' is not a valid Python operator. It should be '>='. Testing with 75 only runs the else branch, so a second test such as 80 or more is needed to cover the if branch.

Student with a class code? Join your class →