Branch Coverage

branch_coverageBranch coverage is one of the key code coverage metrics NCover provides to help development teams determine the overall health and quality of their code base.  Branch coverage builds upon sequence-point coverage, NCover’s base code coverage metric for calculating code coverage statistics.

Branch coverage  is an extremely useful metric for determining how well the code base for a .NET application has been tested.

Definition of Branch Coverage

Branch coverage measures the fraction of independent code segments that were executed. Independent code segments are sections of code that have no branches into or out of them. These independent code segments are sections of code that you would expect to execute in its entirety every time it’s run.

For example, an “if” and “else” condition would represent 2 distinct branches:

// Branch 1
 if (ConditionMet)
   {
     FunctionA();
     FunctionB();
   }
// Branch 2
   else 
   {
     FunctionB();
     FunctionC();
   }

This is obviously a very simple example and the number of branches, and the overall complexity of the code, can quickly grow as additional conditions are introduced.

Branch Coverage Is How We Measure Success And Sequence-Point Coverage Is How We Achieve Success

A key goal of code coverage is to give you confidence in how well your tests are exercising your code base.  The more of your code you are able to test, the greater your confidence will be in your code base.  In other words, greater code coverage leads to greater overall success.

  • The primary value of branch coverage is to measure how much of your program’s structure is being exercised by your tests.
  • The primary value of sequence point coverage is to find the lines of code which are not being executed and are contributing to a lack of coverage.

By relying on branch coverage  to measure how well testing efforts are succeeding and sequence-point coverage to highlight exactly which lines of code are not being executed, you can confidently and reliably develop high quality applications.

An Example Of The Need For Branch Coverage

Let’s look at two simple examples of code as we seek to demonstrate the importance of branch coverage.  Both of these examples, and their resulting code coverage metrics, were generated in NCover Bolt.

Example 1

BranchAndSequencePointResults-1

Example 2

BranchAndSequencePointCode2BranchAndSequencePointResults-2

From a complexity, or branch coverage perspective, both Example 1 and Example 2 have a total of 4 branch points with 3 of those points being covered.  The result for both examples is 75% branch coverage.

However, you can see that in Example 2 we introduced more lines of code to accomplish the same outcome.  This increased the total number of sequence points and the overall sequence point coverage from 70% to 75%.  When relying upon code coverage numbers, you obviously do not want your code coverage percentages to change depending on how many lines of code you use to write a function. Code coverage percentages should be related to the complexity of the code, hence the need for the branch coverage metric.

Trackbacks

  1. […] Branch Coverage (Kerry Meade) […]