# How do we calculate Statement coverage, Branch coverage , Path coverage and Condition coverage in White box testing?

I am preparing for a testing certification. These types of questions are asked frequently.

``````Example 1:
IF P+Q > 100 THEN
Print “Large”
ENDIF
If P > 50 THEN
Print “P Large”
ENDIF

Example 2:
I F X>Y THEN Z = 0
ENDIF

Example 3:
3 Z =X + 2*Y
4 IF Z> 50 THEN
5 PRINT large Z
6 ENDIF
``````

What are Nodes and Edges in the flowchart?

``````Statement Coverage: 1A-2C-3D-E-4G-5H = 1
Branch Coverage: 1A-2C-3D-E-4G-5H , 1A-2B-E-4F = 2
Path Coverage: 4
1A-2B-E-4F
1A-2B-E-4G-5H
1A-2C-3D-E-4G-5H
1A-2C-3D-E-4F
``````

In these example, how do we identify that which is a statement, branch and condition? How do we measure coverage? Please give detailed explanations of ALL 3 examples.

• What are your answers and how do you think the correct answers were obtained? Why are the study guides not sufficient? – Kate Paulk Jun 21 '16 at 15:41
• Removing the manual-testing, test-management, test-design tags because the question is not about testing; the "techniques" tag because "techniques" could mean anything; and the "code-review" tag because the question is about code review. – user246 Jun 26 '16 at 18:31

Before doing control flow test, you need to draw a flowchart, then choose a coverage to work with.

For statement coverage, you need to find paths (from start to end of flowchart) that go through all statements and the number of these paths is the smallest. In other words, if you run all paths you find out, all statements will be executed and the number of these paths is the smallest.

For branch coverage, all paths you find out must cover all the lines.

For path coverage, you need to find all possible paths from start to end of the flowchart. There are 2 ways from start to E, 2 ways from E to end, then the total is 2*2 = 4.

Let's start with the basic stuff. Your provided flow chart is simply another representation of example code 1. Every control flow is represented as edge (here line) and connects always two nodes (here statements).

In contrast to black-box testing you know the program code that you want to test in white-box testing. In your example you take the code under test and determine its statement, branch and path coverage. It is always helpful to do that with a graphical representation of your code as @vhreal stated. In general whatever coverage you have, you try to maximize it (means 100% coverage).

How do you do it? From the beginning of your program (flow chart) you try to reach the end of your program. That is called path. In between you try to reach a 100% coverage of whatever you are currently looking at. Let's do it once for the statements.

• Statement 1 is your starting point. The only outgoing edge of statement 1 is A -> 1A
• Statement 2 is a condition. We have two possible ways to go -> 2B and 2C
• If you took 2B the branching ends in end if. There is only edge E to go on -> E
• If you took 2C you reach statement 3. Again only one outgoing edge D -> 3D
• ...

As result we have 4 different ways of going through the flow chart:

1. 1A -> 2B -> E -> 4F (= 60% statement coverage)
2. 1A -> 2B -> E -> 4G -> 5H (= 80% statement coverage)
3. 1A -> 2C -> 3D -> E -> 4F (= 80% statement coverage)
4. 1A -> 2C -> 3D -> E -> 4G -> 5H (= 100% statement coverage)

As your objective is to reach a 100% coverage rate with the smallest number of test cases you would only take path 4. That was easy but it would be more difficult to decide what paths to take for a overall 100% statement coverage if each of the 4 paths would have a statement coverage <100%.

• Statements : Simply, these are what you have in the boxes and diamond shapes.
• You can cover all the statements in the flowchart by writing 1 Test Case that follows the following route
• 1A-2C-3D-E-4G-5H.
• Therefore, the Statement Coverage is 1

• Branches/Decisions : Decisions that you can take in the process flow diagram (For example, if you consider the statement 2, there are two branches to it)
• You can cover all the branches in the flowchart by writing 2 Test Cases that follow the following two routes
• 1A-2C-3D-E-4G-5H
• 1A-2B-E-4F
• Therefore, the Branch Coverage is 2

• Paths : paths that you can take to travel in the flow chart from the start to the end.

• You can cover all the paths in the flowchart by writing 4 Test Cases that follow the following four routes
• 1A-2B-E-4F
• 1A-2B-E-4G-5H
• 1A-2C-3D-E-4G-5H
• 1A-2C-3D-E-4F
• Therefore, the Path Coverage is 4

Coverage

Coverage can be calculated for all the three techniques mentioned above.

• Statement coverage
• Let us say your test case covers the following route - 1A -> 2B -> E -> 4F
• There are total of 5 Statements in your flowchart
• You cover only 3 Statements
• Calculate the Statement Coverage - (3/5)*100
• Your Test Case, therefore, has 60% of Statement Coverage

• Branch coverage
• Let us say your test case covers the following route - 1A -> 2B -> E -> 4F
• There are total of 4 Branches/Decision in your flowchart (2 decisions for Statement no 2 and 2 decisions for Statement no 4)
• You cover only 2 Branches/Decisions
• Calculate the Branch/Decision Coverage - (2/4)*100
• Your Test Case, therefore, has 50% of Statement Coverage

• Path coverage
• Let us say your test case covers the following route - 1A -> 2B -> E -> 4F
• There are total of 4 Paths in your flowchart
• You cover only 1 Path
• Calculate the Path Coverage - (1/4)*100
• Your Test Case, therefore, has 25% of Path Coverage