Free Unit Testing Code Coverage with OpenCover and ReportGenerator
So you want to do development "right," you write unit tests and now want to see your code coverage? But you're not using a Visual Studio Enterprise?
Phil Fresle comes to the rescue, showing off how you can use a number of free projects to get your code coverage reports today...
The usual choices for getting code coverage metrics in Visual Studio is to buy the Enterprise version of Visual Studio or buy the dotCover third party tool, both of which are costly, especially if you are developing small applications yourself and want to check the coverage of your unit tests.
The problem I found, however, was the documentation available to get the tool working was less than adequate for my purposes and it took several hours of faff to get it working for me. To save you time and money I have documented my experience here.
The following were all referenced in the creation of this document:
I have tested that this works in Visual Studio 2013 Professional and Visual Studio 2015 Community Edition, with both MSTest and NUnit.
I assume you already have a solution with application project that you are testing with a test project, and where you are using NUnit for that testing that you have already included the NUnit NuGet package.
OpenCover is a code coverage tool for .NET 2 and above (Windows OSs only - no MONO), with support for 32 and 64 processes and covers both branch and sequence points. OpenCover was started after attempts to make PartCover support 64-bit processes got just a little too complicated.
OpenCover produces an XML output file that can be used to generate a graphical report. Thankfully you do not have to do that yourself as Daniel Palme has kindly added support for OpenCover to his Report Generator tool.
This the output from Report Generator for OpenCover.Framework.dll looks something like this.
ReportGenerator converts XML reports generated by OpenCover, PartCover, Visual Studio or NCover into human readable reports in various formats. http://danielpalme.github.io/ReportGenerator
ReportGenerator converts XML reports generated by OpenCover, PartCover, Visual Studio or NCover into human readable reports in various formats.
The reports do not only show the coverage quota, but also include the source code and visualize which lines have been covered.
ReportGenerator supports merging several reports into one. It is also possible to pass one XML file containing several reports to ReportGenerator (e.g. a build log file).
The following output formats are supported by ReportGenerator:
- HTML, HTMLSummary, HTMLChart
- XML, XMLSummary
- Latex, LatexSummary
- Custom reports
- OpenCover (Nuget)
- PartCover 4.0 (Nuget)
- PartCover 2.2, 2.3
- dotCover (Nuget, /ReportType=DetailedXML)
- Visual Studio (vstest.console.exe, CodeCoverage.exe)
- NCover (tested version 1.5.8, other versions may not work)
Also available as NuGet package: https://www.nuget.org/packages/ReportGenerator