Skip to content
IGCSE·Tuition
Computer Science · Lessons

Identify an untested branch

Your tests all pass, and yet one part of the program has never actually run.

On this page
  1. How do I list the branches?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where does this lead next?

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:

BranchCondition that reaches it
AScore >= 80
BScore < 80 and Score >= 50
CScore < 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.

BranchReached by a test?
AYes (85)
BYes (70)
CNo

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.

Questions people ask

What is a branch in a program?

A branch is one of the routes the program can take at a decision. An IF with an ELSE has two branches, and an IF with an ELSE IF and a final ELSE has three. Each branch is a separate piece of code that may or may not run for a given input.

How do I find an untested branch?

List every branch, then run each test input through the code and mark the branch it reaches. Any branch with no tick is untested. Then choose a new input that makes that branch's condition true.

Does passing all my tests mean the program is correct?

No. Tests only show that the paths they reach work for those inputs. A branch that no test reaches could contain a fault that nobody has seen. That is why a good test set aims to reach every branch.

Do I need a test for each boundary as well?

Yes. Reaching every branch is the minimum. Boundary values then check that the conditions are written correctly, such as 80 reaching the grade A branch if the rule says 80 or more.

Updated:

Your next step

If your tests always seem to pass but you cannot be sure the whole program is covered, a one-to-one teacher can walk through your code with you and map each path to a test.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service