Code coverage is a good tool to force you to keep maintaining and adding unit and integration tests to your solution. It also increases your confidence in making changes if you can see that the code your changing is actually covered by tests both before and after your changes.
Problem is, integrated code coverage in Visual Studio only comes with the Premium version and not all of us has an extra 3500$ to spend just to get some nice code statistics (admittedly that’s not all you get but it is the main feature of VS Premium I’ve used).
So, are there any options? Why yes! I’ve recently started using OpenCover in conjunction to ReportGenerator. Both really good solid libraries to run tests, output coverage and then format it into a nice and clean HTML format.
1. Add the nuget packages:
Install-Package OpenCover Install-Package ReportGenerator
2. And then add this as a post build or maybe a bat file (I used it in a bat file myself since I didn’t want to trigger this for every build):
mkdir reports "......packages\OpenCover.4.5.2506\OpenCover.Console.exe"^ -target:"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe"^ -output:reportsoutput.xml^ -register:user^ -targetargs:"/noresults /noisolation /category:UnitTest /testcontainer:IndentationSplitter.tests.dll" "......packages\ReportGenerator.22.214.171.124\ReportGenerator.exe"^ -reports:reportsoutput.xml^ -targetdir:reports "reportsindex.htm"
3. Replace the IndentationSplitter.tests.dll with your own dll to run the tests from and any category filters. I’ve tagged all my tests with TestCategory(“UnitTest”) to make sure this runs quickly.
4. You might also need to change the folder names for the OpenCover and ReportGenerator depending on what version you’re using.
If you, like me, didn’t know about the ^ at the end of the lines, this signifies that the line is broken to continue on next line. Really helps to make those batch files readable doesn’t it?
Here are some examples of how the reports may look, these below are taken from the running of my latest project the IndentationSplitter: