Types Of Code Coverage- Examples In C#

Types Of Code Coverage- Examples In C#

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();
}

Related Articles

Development, Resources

Most Complete NUnit Unit Testing Framework Cheat Sheet

An essential part of every UI test framework is the usage of a unit testing framework. One of the most popular ones in the .NET world is NUnit. However, you can

Most Complete NUnit Unit Testing Framework Cheat Sheet

Development, Resources

Most Complete MSTest Unit Testing Framework Cheat Sheet

An essential part of every UI test framework is the usage of a unit testing framework. One of the most popular ones in the .NET world is MSTest. However, you ca

Most Complete MSTest Unit Testing Framework Cheat Sheet

Development

Assert DateTime the Right Way MSTest NUnit C# Code

NUnit has added built-in support for this using the keyword Within.

Assert DateTime the Right Way MSTest NUnit C# Code

Development

Optimize C# Reflection Up to 10 Times by Using Delegates

Developers love reflection because it can save them numerous hours of boilerplate code. But developers also know reflection is slow and it should be used with c

Optimize C# Reflection Up to 10 Times by Using Delegates

Development

Get Property Names Using Lambda Expressions in C#

In this article, I am going to present to you how to get property and method names from lambda expressions. You can pass properties and methods as methods' para

Get Property Names Using Lambda Expressions in C#

Development

Dynamic – A Bad Practice Turned Great

I had just upgraded My Tested ASP.NET to be compatible with version 2.2 of ASP.NET Core, when the server-side technology upgraded to version 3.0. I had to re-do

Dynamic – A Bad Practice Turned Great
Anton Angelov

About the author

Anton Angelov is Managing Director, Co-Founder, and Chief Test Automation Architect at Automate The Planet — a boutique consulting firm specializing in AI-augmented test automation strategy, implementation, and enablement. He is the creator of BELLATRIX, a cross-platform framework for web, mobile, desktop, and API testing, and the author of 8 bestselling books on test automation. A speaker at 60+ international conferences and researcher in AI-driven testing and LLM-based automation, he has been recognized as QA of the Decade and Webit Changemaker 2025.