Click here to Skip to main content
Click here to Skip to main content

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

, 16 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Branch Level Code Coverage with NUnit, OpenCover, ReportGenerator and Visual Studio Integration

Introduction

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: http://www.nunit.org/index.php?p=consoleCommandLine&r=2.2.5
  2. OpenCover captures the NUnit testing results and produces the coverage report in .XML format. Usage reference: https://github.com/OpenCover/opencover/wiki/Usage
  3. ReportGenerator produces the HTML report based upon OpenCover’s Results.XML. Usage Reference: https://reportgenerator.codeplex.com/
  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.

Note: In this batch file, replace OpenCover.4.5.2506, ReportGenerator.1.9.1.0 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
@ECHO OFF
..\..\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.1.9.1.0\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.

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 necessary toolbar (I configured to Build tool bar) as shown in the below steps.

Sample Output

License

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

About the Author

Sabarinathan A
Technical Lead Tech Mahindra
India India
I have been programming for last 10 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 http://sabarinathanarthanari.com
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Mar 2014
Article Copyright 2014 by Sabarinathan A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid