What is Statement coverage in testing?

| | 2 min read

Statement coverage is a white box testing technique, which involves the execution of all the statements at least once in the source code. It is a metric, which is used to calculate and measure the number of statements in the source code which have been executed.

Using this technique we can check what the source code is expected to do and what it should not. It can also be used to check the quality of the code and the flow of different paths in the program. The main drawback of this technique is that we cannot test the false condition in it.

(Statement coverage = No of statements Executed/Total no of statements in the source code * 100)

Example:

Read A
Read B
if A > B
  Print “A is greater than B”
else
  Print "B is greater than A"
endif

Set 1: If A = 5, B = 2

  • No of statements Executed: 5
  • Total no of statements in the source code: 7
  • Statement coverage =5/7*100 = 71.00 %

Set 2: If A = 2, B = 5

  • No of statements Executed: 6
  • Total no of statements in the source code: 7
  • Statement coverage =6/7*100 = 85.20 %

This is purely a white box testing method. It tests the software’s internal coding and infrastructure and so the programmer is the one who should take the initiative to do this. This technique is very suitable for Drupal programmers and other programmers.