A program with decisions has several routes through it. An untested branch is a route that none of your test data has ever followed. Faults often hide there, because the code in it has never been run.
This lesson is the last in the validation, verification and testing module. It builds on predicting expected results.
How do I list the branches?
For each IF, write down every route. Then give each route the condition that sends the program down it. For a three-way grade rule:
| Branch | Condition that reaches it |
|---|---|
| A | Score >= 80 |
| B | Score < 80 and Score >= 50 |
| C | Score < 50 |
Branch C has no condition written in the code. It runs when both earlier conditions are FALSE, so you must work out the input yourself.
Worked example
A fictional program should give grade A for 80 or more, B for 50 to 79 and C for below 50.
INPUT Score
IF Score >= 80 THEN
Grade ← "A"
ELSE
IF Score >= 50 THEN
Grade ← "B"
ELSE
Result ← "C"
ENDIF
ENDIF
OUTPUT Grade
A tester runs two tests: Score = 85 and Score = 70.
Step 1, trace Score = 85. 85 >= 80 is TRUE, so Grade ← “A”. Output: A. Expected A. Pass. This reached branch A.
Step 2, trace Score = 70. 70 >= 80 is FALSE. Then 70 >= 50 is TRUE, so Grade ← “B”. Output: B. Expected B. Pass. This reached branch B.
Step 3, mark the table.
| Branch | Reached by a test? |
|---|---|
| A | Yes (85) |
| B | Yes (70) |
| C | No |
Step 4, add a test for branch C. Choose Score = 40. 40 >= 80 is FALSE and 40 >= 50 is FALSE, so branch C runs. The line is Result ← "C", so Grade is never set. The output is empty or undefined. Expected C. Fail.
The fault was invisible until the missing branch was tested. The line should read Grade ← "C".
Then add the boundary tests 80, 79, 50 and 49 to check the comparisons. Each should give A, B, B and C in that order.
The mistake to watch for
Mistaken conclusion: “Both tests passed, so the program works.”
The student looked at the results and not at which code the tests reached.
A passing test only tells you about the path it ran. Before concluding, count the branches and count the paths your tests covered. If the numbers differ, the program is not fully tested.
Check yourself
1. An IF has an ELSE IF and a final ELSE. How many branches are there?
Show answer
Three: the IF branch, the ELSE IF branch and the ELSE branch.
2. In the grade program, which test value reaches branch C?
Show answer
Any score below 50, for example 40 or 0. A boundary choice would be 49.
3. Tests: 90 and 95 only. Which branches are untested?
Show answer
Both tests reach branch A. Branches B and C are untested.
Where does this lead next?
Test yourself on every idea in the module with the mixed practice set. To return to the start of the module, see choosing a validation rule. The restricted pseudocode trace trainer is useful for tracing each branch.
If you would like help building complete test plans, that is something we work on in online one-to-one Computer Science tuition.