Software Testing - Branch/Decision Testing
Branch testing and decision testing are closely related. We will treat them same. When 100% coverage is concerned, the two techniques are the same.
Branch and decision testing require examination of the source code and the creation of tests that will exercise individual branches of code. A branch is a route from a conditional statement. On a flowchart a branch is a line leaving a decision diamond. For 100% Branch Coverage we should test all branches in a piece of code, we need to achieve all possible results from all of the decision statements.
Take, as an example, the following source code:
Read a // Executable Read b // Executable IF a > b THEN // Executable Print "a > b" // Executable IF a > 10 THEN // Executable Print "a > 10" // Executable ENDIF ELSE Print "a =< b" // Executable ENDIF
This is the code we used in Statement Testing example. The code reads two numbers into variables (a and b). It then prints out a message indicating which number is the larger. If the number in variable a is larger than the number in variable b then the number a is compared to the number 10.
Executable code is indicated in the comments. The ELSE and ENDIF statements are considered to be part of earlier IF statements.
We can represent this code in a flow chart. Every box in the flowchart represents an individual statement.
As before, we shall run the code with the values: 30 and 20. We expect the output to look like:
a > 10
The exercised branches are highlighted in the flowchart below:
This test has exercised two of the four branches in the code, achieving 50% Branch Coverage.
For our second test we shall run the code with the values: 20 and 30. We expect the output to look like:
a <= b
This test exercises one new branch of the code as highlighted in the following flowchart. We have not yet exercised the N branch from the a > 10 test.
We have now exercised 3 of the 4 branches, achieving 75% Branch Coverage.
To exercise the remaining branch, we shall run the code with the values: 3 and 2. We expect the output to look like:
a > b
The exercised branches are highlighted below.
By performing all three tests (Tests 1, 2 and 3) we can test all branches of the code. In this case, 100% branch testing requires one more test than 100% statement testing. This is because statement testing does not require us to test branches where there are no executable statements. It can be shown that 100% Statement Coverage requires no more tests than are required for 100% Branch Coverage.