Click here to Skip to main content
14,871,356 members
Articles / All Topics
Posted 16 Mar 2014

Tagged as


8 bookmarked

Branch Level Code Coverage with NUnit, OpenCover, ReportGenerator and Visual Studio Integration

Rate me:
Please Sign up or sign in to vote.
3.91/5 (6 votes)
16 Mar 2014CPOL2 min read
Branch Level Code Coverage with NUnit, OpenCover, ReportGenerator and Visual Studio Integration


For one of the projects, we needed a branch level code coverage report to be generated. Visual Studio 2012 supports code coverage with different unit testing frameworks. But Visual Studio is not producing the branch level code coverage details.

We are using the NUnit as unit testing framework for the project. So we analysed the different code coverage tools available for .NET projects. We found the OpenCover with ReportGenerator is the best option available with complete NUnit features (the NCover is yet to support the TestCaseSource Attribute).

But it lacks IDE integration. So I found a way to generate and invoke the reports from Visual Studio toolbar.

  1. Basically, the script first invokes the NUnit unit testing tool. Command line console reference:
  2. OpenCover captures the NUnit testing results and produces the coverage report in .XML format. Usage reference:
  3. ReportGenerator produces the HTML report based upon OpenCover’s Results.XML. Usage Reference:
  4. Invoke the HTML summary report created in coverage sub-folder.

Install the Necessary Nuget Packages

  • NUnit.Runners
  • OpenCover
  • ReportGenerator
PM> install-package Nunit.Runners
PM> install-package OpenCover
PM> install-package ReportGenerator

Create Simple Batch File to be Invoked from Visual Studio

  1. Create the following .bat file in unit test project with the name Coverage.bat.
  2. Change the “Copy to Output Directory” property to “Copy always” value. This will copy the file to bin folder or unit test project.

    Image 1

Note: In this batch file, replace OpenCover.4.5.2506, ReportGenerator. with the versions deployed in your project.

REM @echo suppresses command line. ^ is line continuation character
REM $(BinDir) is VS command line parameter of bin folder path of the selected project
..\..\packages\OpenCover.4.5.2506\OpenCover.Console.exe ^
 -target:..\..\packages\NUnit.Runners.2.6.3\tools\nunit-console.exe ^
-targetargs:"<ASSEMBLY>.UnitTest.dll /noshadow /nologo" ^
-register:user ^
-filter:"+[<assembly>*]* -[<assembly>.DataModel]* -[<assembly>.UnitTest]* -[<assembly>.Diagrams]*" ^
-filter:-excludebyfile:*\*Designer.cs -mergebyhash
REM Default filters are: -[mscorlib]* -[mscorlib.*]* -[System]* -[System.*]* -[Microsoft.VisualBasic]*
REM delete old coverage files
REM /F /Q switches to delete files and directories even with readonly attribute without confirmation
DEL /F /Q .\coverage\*.*
REM Generate HTML based coverage reports
..\..\packages\ReportGenerator.\reportgenerator.exe ^
 -reports:results.xml -targetdir:coverage Verbosity: Error
REM invoke the html coverage summary in browser
START ".\coverage\index.htm"

Create the External Tool Entries in Visual Studio and in Toolbar

Configure the “External Tools” to Coverage.Bat in bin folder as shown in the below screenshot. This will execute the commands within the IDE and will display the output in Visual Studio’s “Output” window.

Image 2

If you configured this as first External Tool, this will be displayed as “External Command 7? (by default, Visual Studio has 6 external tools pre-configured). You can add this menu item to the necessary toolbar (I configured to Build tool bar) as shown in the below steps.

Image 3

Image 4

Image 5

Sample Output

Image 6

Image 7


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


About the Author

Sabarinathan A
India India
I have been programming for last 20 years on various platforms including .NET, Visual Basic 6, Oracle and SQL server.

I decided to write articles when I have free time hoping to share my thoughts with you.

To know more about me visit

Comments and Discussions

-- There are no messages in this forum --