Test coverage of branches and loops

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

This Test coverage of branches and loops worksheet is part of the ACARA Digital Technologies Years 9–10 — Testing and Validating Programs journey. Print it as a booklet?

Test coverage of branches and loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
3. Which loop test checks that a for loop copes with an empty list?
A Running it many times
B Running it zero times
4. Full loop coverage means testing a loop running...
A only the maximum number of times
B zero times, once and many times
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

Match the Pairs

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

Left columnRight column
1if mark >= 50: print("Pass") else: print("Fail") AThe 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.
2for item in cart: total += item BTest the loop with an empty list (zero times), a one-item list (once) and a longer list (many times).
3Every branch of an if/elif/else needs its own test value, so three branches need at least three tests. Cif score >= 80: grade = "A" elif score >= 50: grade = "B" else: grade = "C"
4A loop that never runs when given an empty list shows the zero-times case, and any code inside it stays untested. DOnly age 20 was tested, so the else branch never ran. The Minor line is untested code. Add a test such as age 10.
5# Tests run: age = 20 only if age >= 18: print("Adult") else: print("Minor") ENeeds at least two tests for full branch coverage: one mark that is 50 or more and one that is below 50.
6# Tests run: count = 5 only while count > 0: count -= 1 Ffor n in []: print(n)

Definitions

test coverage

Fill in the Blanks

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

1.
if mark >= 50: print("Pass") ____:
2.
items = [] ___ x in items: print(x)
3.
count = 0 for i in range(_): count += 1
4.
for n in [2, 4, 6]: total __ n print(total)
5.
if age < 13: print("Child") ____ age < 20:

Spot the Error

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

Question 1
A def sign(n):
B if n > 0
C return 'pos'
D elif n < 0:
E return 'neg'
F return 'zero'
Question 2
A items = []
B total = 1
C for x in items:
D total += x
E print(total)
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
Question 4
A def total(nums):
B t = 0
C for n in nums:
D t = n
E return t
F print(total([]))
Question 5
A mark = 75
B if mark => 80:
C print('High')
D else:
E print('Low')

Answer Key

Test coverage of branches and loops
Section A: Quick Questions
Q1
A — At least one test that runs the if, one that runs the elif and one that runs the else
Q2
A — The else branch
Q3
B — Running it zero times
Q4
B — zero times, once and many times
Q5
A — That line is untested and could hide a bug
Section B: Match the Pairs
Q1
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.
Q2
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).
Q3
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"
Q4
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)
Q5
# 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.
Q6
# 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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
if mark >= 50: print("Pass") else:
Q2
items = [] for x in items: print(x)
Q3
count = 0 for i in range(1): count += 1
Q4
for n in [2, 4, 6]: total += n print(total)
Q5
if age < 13: print("Child") elif age < 20:
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.