What is Statement Coverage?

Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.

What should I know to learn Statement Coverage?

Basic knowledge of algorithm and flowchart functionality would be helpful for anyone who want to execute statement coverage.

 

Statement Coverage

Statement coverage is used to derive scenario based upon the structure of the code under test.

Statement Coverage in Software Testing

In white box testing, the tester is concentrating on how the software works. In other words, the tester will be concentrating on the internal working of source code with respect to control flow graphs or flow charts.

Also, statement testing is concerned with exercising the loop in the software by referring to either source code or control flow charts.

Different test cases may be derived to exercise the loop once, twice and many times. This may be done regardless of the functionality of the software.

Structural based testing is also referred as 'Code Coverage Technique' which can be used by all levels of testing. Developers use structural based techniques in component testing or module testing or unit testing, component integration testing.

Testing can reveal a fault or defect only when the execution of that corresponding faulty element causes a failure. For example, if there is a fault in the statement at line 10 of the source code, it could be revealed by the test cases which has potential test steps or test data to reveal the defect in the source code statement.

Generally in any software, if we look at the source code there will be a wide variety of elements included in operators, functions, looping, exceptional handlers and etc. But given a set of instruction to perform a task. All the possible path's, line and statement with respect to the task should be covered.

Let's understand this with an example, how to calculate statement coverage.

Formula to calculate Statement Coverage in the Source Code

The statement coverage can be calculated as shown below

Statement Coverage in Software Testing

Scenario to calculate Statement Coverage for a given source code. Here we are taking two different scenarios to check the percentage of statement coverage for each scenarios.

Scenario 1 Source Code:

Printsum (Int a, Int b) {                       ------------  Printsum is a function 
    Int result = a+ b; 
    If (result> 0)
    	Print ("Positive", result)
    Else
    	Print ("Negative", result)
    }                                              -----------   End of the source code 

Solution for Scenario 1:

If A = 3, B = 9

Printsum (Int a, Int b) {                          
	Int result = a+ b; 
    If (result> 0)
    	Print ("Positive", result)
   	Else
    	Print ("Negative", result)
    }					

The statements marked in blue colour are those which are executed as per the scenario

Number of executed statements = 5, Total number of statements = 7

Statement Coverage in Software Testing

Statement Coverage: 5/7 = 71%

Statement Coverage in Software Testing

Likewise we will see scenario 2,

Solution for Scenario 2:

If A = -3, B = -9

Printsum (Int a, Int b) {                           
	Int result = a+ b; 
    If (result> 0)
    	Print ("Positive", result)
    Else	
    	Print ("Negative", result)
    }					

Number of executed statements = 6

Total number of statements = 7

Statement Coverage in Software Testing

Statement Coverage: 6/7 = 85%

Statement Coverage in Software Testing

But overall if you see, all the statements are being covered by 2nd scenario's considered. So we can conclude that overall statement coverage is 100%.

Statement Coverage in Software Testing

What is not covered by Statement Coverage?

  1. Missing Statement in the source code

What is covered by Statement Coverage?

  1. Unused Statements
  2. Dead Code
  3. Unused Branches

Conclusion

Statement testing forces the developer to reason carefully before complete implementation of the design specification. It reveals an error in the hidden code but doesn't reveal missing code or statement.

Statement testing is expensive in terms of both time and money required to perform white box testing.