Code coverage analysis is used to measure the quality of software testing, usually using dynamic execution flow analysis. There are many different types of code coverage analysis, some very basic and others that are very rigorous and complicated to perform without advanced tool support. Most people believe that they have tested everything when Visual Studio Code Coverage shows 100%. But this is so wrong! Here, I will give you a couple of examples of the different code coverage types.
Statement Code Coverage
The most basic type of code coverage is the Statement Code Coverage. Yeah, you guessed it right, this is the type of code coverage performed by most IDEs (Visual Studio including).
private void PeformAction(int x, int y)
{
this.DoSomething();
if (x > 3 && y < 6)
{
this.DoSomethingElse();
}
this.DoSomething();
}
Branch Code Coverage (Decision Coverage)
Every decision is executed with both possible outcomes - TRUE and FALSE. For Loops- bypass it, execute the body and return to the beginning.
private void PeformAction(int x, int y)
{
this.DoSomething();
if (x > 3 && y < 6)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
}
private void PeformAction(int x, int y, bool shouldExecute)
{
if (shouldExecute)
{
this.DoSomething();
if (x > 3 && y < 6)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
}
}
Branch Condition Coverage
Sometimes your conditions depend on several smaller conditions, called atomic parts. An atomic is a condition that has no logical operations such as AND, OR, NOT, but can include ”<”, ”>” or ”=”. One condition can consist of multiple atomic parts.
private void PeformAction(int x, int y, int z)
{
this.DoSomething();
if ((x > 3 && y < 6) || z == 20)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
}
private void PeformAction(int x, int y)
{
this.DoSomething();
if (x > 3 && y < 6)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
}
Branch Condition Combination Code Coverage
3 <= x (F) && x < 5 (F) - the combination not possible because the value x shall be smaller than 3 and greater or equal to 5 at the same time.
private void PeformAction(int x, int y)
{
this.DoSomething();
if (3 <= x && x < 5)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
}
Condition Determination Coverage
This coverage eliminates the previously discussed problems. Not all combinations of the atomic parts should be applied. Only the combinations where the modification of the logical value of the atomic part can change the logical value of the whole condition.
private void PeformAction(int x, int y)
{
this.DoSomething();
if (4 <= x || y < 6)
{
this.DoSomethingElse();
}
else
{
System.Console.WriteLine(x + y);
}
this.DoSomething();
} 