Click here to Skip to main content
15,847,750 members
Articles / .NET

Getting code coverage from your .NET testing using OpenCover.

Rate me:
Please Sign up or sign in to vote.
4.85/5 (15 votes)
3 Nov 2013CPOL3 min read 120.6K   1.3K   21   9


OpenCover is a free, open-sourced, code coverage tool for .NET 2.0 and above running on the .NET platform. It supports sequence coverage, branch coverage and has a cover by test facility. Though OpenCover is command line only, a rich HTML UI of the results can be visualized using ReportGenerator

We will aim to demonstrate how you can use this utility to get visibility into your testing coverage.  


OpenCover is currently the only actively developed and maintained open-sourced tool of it's type for the .NET platform but it was not the first, some of the others being:  

NCover - Probably the most well-known commercial tool for .NET code coverage started life as an open-source project.  

Coverage.eye - Originated at Microsoft and was available on gotdotnet, the repository/sample has since gone and no full mirrors appear to exist (The wayback machine only has the text).   

PartCover - This appears to have been created in response to NCover going commercial and though actively used it has limitations e.g. 32-bit only. The original repository is no longer being maintained by its original developers and is now being maintained on github

Using OpenCover 

Preparing for OpenCover

The following steps detail how to download OpenCover (and other supporting tools) from NuGet.  

OpenCover is available as a zip or msi download via its bitbucket mirror but for the sake of this article we will use the OpenCover nuget package.  

  1. Create a new solution
  2. For that solution "Enable NuGet Package Restore"
  3. Using "Manage NuGet Packages for Solution" add the following packages

Once completed you should have a solution with a .nuget folder which contains a packages.config that looks something like the following 

<?xml version="1.0" encoding="utf-8"?> 
  <package id="NUnit.Runners" version="2.6.3" />
  <package id="OpenCover" version="4.5.1923" />
  <package id="ReportGenerator" version="" />
</packages>  <span style="color: rgb(17, 17, 17); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;"> </span>


  1. Create a C# project (class library) called Sample 
  2. Create a C# project (class library) called Sample.Test and reference the Sample project 
  3. Use NuGet to add the following package 

Now to add a simple class and method and a test that will exercise that method.

In the Sample project add the following class  

public class Target
    public static void DoSomething()
            Console.WriteLine("I ran!");
        catch (Exception ex)

In the Sample.Test project add the following class   

using NUnit.Framework;
public class TargetTest
    public void DoSomethingTest()

Running tests with OpenCover

OpenCover does not directly execute your tests but instead needs to execute another application that executes your tests in this case we are using NUnit. 

First lets create a batch file that we can execute our tests from the command line 

..\..\..\packages\NUnit.Runners.2.6.3\tools\nunit-console.exe sample.test.dll /noshadow

This batch file can be added to the Sample.Test project (with Copy to Output Directory set to Copy Always). 

Now we can use OpenCover to execute this batch file, again we can add this as a a batch file to our Sample.Test project so that we can execute it on the command line

..\..\..\packages\OpenCover.4.5.1923\OpenCover.Console.exe -target:runtests.bat -register:user -filter:+[Sample]* 

The arguments are 

-target: - used to indicate the target process for OpenCover to execute.

-register:user - used to register the COM objects that OpenCover uses to instrument your assemblies. 

-filter: - used to control which assemblies OpenCover will gather coverage data for. The filter is capable of including and excluding assemblies and classes and is actually the same filter format that PartCover uses. The filter is one of the more complex features of OpenCover and more detail is provided with the documentation that is installed alongside OpenCover or in the Usage wiki

When executed, OpenCover will produce an XML file (default results.xml) that contains all the data related to that test run. To visualize that data we can use ReportGenerator to produce some rich HTML output. ReportGenrator can also be run on the command line  

..\..\..\packages\ReportGenerator.\reportgenerator.exe -reports:results.xml -targetdir:coverage  

If we open the produced coverage output (coverage\index.htm) we can see the visualization of the coverage of our target code.  

 7 namespace Sample
 8 {
 9     public class Target
 10     {
 11         public static void DoSomething()
 112         {
 13             try
 14            {
 15                 Console.WriteLine("I ran!");
 16             }
 17             catch (Exception ex)
 18             {
 19                 Console.WriteLine(ex.Message);
 20             }
 21         }
 22     }
 23 } 

As you can see it is quite simple to understand what code your tests actually cover.

For a real-world example OpenCover is actually used to gather coverage on it's own tests and the results can be seen on the OpenCover build pipeline.  


Nov 3rd 2013 - Original Article  



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Employed (other) Purplebricks
Australia Australia
All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.

Now living and working in Australia, trying to be involved in the local .NET and Agile communities when I can.

I spend a good chunk of my spare time building OpenCover and maintaining PartCover both of which are Code Coverage utilities for .NET.

Comments and Discussions

QuestionOpenCover Branch Unit Test Coverage results in SonarCloud Pin
Member 1515185614-Apr-21 6:59
Member 1515185614-Apr-21 6:59 
QuestionWhats the benefit of Nuget here? Pin
Mike (Prof. Chuck)5-Jul-19 23:35
professionalMike (Prof. Chuck)5-Jul-19 23:35 
AnswerRe: Whats the benefit of Nuget here? Pin
Ansgar Hellwig3-Apr-20 1:42
professionalAnsgar Hellwig3-Apr-20 1:42 
QuestionVisualStudio integration Pin
axodox11-Jan-17 12:45
axodox11-Jan-17 12:45 
QuestionHow to use OpenCover with Windows Phone 8.1 (RT) Pin
Uilque Messias11-Dec-15 8:48
Uilque Messias11-Dec-15 8:48 
AnswerRe: How to use OpenCover with Windows Phone 8.1 (RT) Pin
Shaun Wilde11-Jul-16 18:19
Shaun Wilde11-Jul-16 18:19 
Questionbatch file Pin
Member 1062133024-Feb-14 5:06
Member 1062133024-Feb-14 5:06 
AnswerRe: batch file Pin
Shaun Wilde25-Feb-14 10:08
Shaun Wilde25-Feb-14 10:08 
GeneralMy vote of 4 Pin
Antonio Petricca7-Nov-13 21:12
Antonio Petricca7-Nov-13 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.