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
But it lacks IDE integration. So I found a way to generate and invoke the reports from Visual Studio toolbar.
- 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
- OpenCover captures the NUnit testing results and produces the coverage report in .XML format. Usage reference: https://github.com/OpenCover/opencover/wiki/Usage
ReportGenerator produces the HTML report based upon OpenCover’s Results.XML. Usage Reference: https://reportgenerator.codeplex.com/
- Invoke the HTML summary report created in coverage sub-folder.
Install the Necessary Nuget Packages
PM> install-package Nunit.Runners
PM> install-package OpenCover
PM> install-package ReportGenerator
Create Simple Batch File to be Invoked from Visual Studio
- Create the following .bat file in unit test project with the name Coverage.bat.
- 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
ReportGenerator.18.104.22.168 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
-targetargs:"<ASSEMBLY>.UnitTest.dll /noshadow /nologo" ^
-filter:"+[<assembly>*]* -[<assembly>.DataModel]* -[<assembly>.UnitTest]* -[<assembly>.Diagrams]*" ^
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
-reports:results.xml -targetdir:coverage Verbosity: Error
REM invoke the html coverage summary in browser
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 the necessary toolbar (I configured to Build tool bar) as shown in the below steps.