Loading

Code Coverage


What is code coverage?

It may be defined as a metric tool, use to measure the coverage of the programming code, in terms of line of code, under the test. It is a type of white-box testing, used to measure the quantity of code covered, during the execution of a program or code.

The chief purpose, behind code coverage, is to ensure that no line of code or area of the program, left unvisited or untouched for the testing purpose. Through code coverage, we may be able to get the answers to the following questions:

  • How many lines of code, is being executed, during the task of test execution?
  • How many lines of code or area of the programming code are still beyond the reach of testing process?
  • How many additional test cases, may be required, to increase the code coverage?
  • Redundant test cases, useless for increasing the code coverage.
Below given is the working formula for determining the code coverage.
Code Coverage = (Numbers of line of code, executed/ Total numbers of line of code) * 100
Higher the coverage, thorough will be the testing, i.e. more the coverage, higher may be the quality. But, it is pertinent to mention that even after going for the 100% code coverage, the quality factor is still not guaranteed.

How to measure the code coverage?

There are various parameters & criteria, to measure the code coverage. Below given are some of the important types:

Function Coverage

Whether, each and every function which is declared and defined, has been called or not during the execution of the program.

Statement Coverage

The simplest form of the code coverage to check that whether, each and every statement not the lines has been brought under the execution during the running of the software program. Here, we have specifically said the statements not the lines, as a statement may be dispersed or covered in multiple lines or the line may contain more than one statement.

Branch Coverage

Here, it is checked that if a node is able to traverse or jump to its all possible branches during the execution of the code.

Condition Coverage

All the elements of a particular condition, i.e. boolean expression have been used and if the given expression is able to produce result in 'Yes' and 'No' form during the execution of the code .

Sometimes, condition coverage may also be seen as the decision coverage, which is the combination of function and branch coverage and used to check that every possible entry and exit points in the programming code has been called upon or not, at least once, and if all the possible outcomes of every decision has been invoked or not during the execution.


Apart from the above mentioned important criteria, following types may also be approach for measuring the code coverage

  • Path coverage
  • Call coverage
  • Data flow coverage
  • Loop coverage
  • State coverage

Tools

Below given are some of the tools to measure the code coverage under the test.

  • Hansel → for Java
  • EMMA→Java
  • Quilt→ Java
  • Jcoverage→Java
  • Clover.Net→.Net
  • Ncover→ .Net
  • Squish Coco→ C/C++
  • Devel::Cover→Perl
  • Large project→Python
  • Pylid→Python
  • PHP Test Coverage Tool→ PHP