Code Coverage - Testing with OpenCover and PartCover
Just some days ago, a new code coverage tool was released for the first time. It's called OpenCover. Shaun Wilde created this tool, since PartCover 4 has some issues that are difficult to resolve with the current code base. He describes some of them on his blog.
In this post I will do a comparison between OpenCover and PartCover and show how these tools can be used to get the coverage of unit tests.
Comparison
OpenCover runs on 32/64 Bit Windows and in contrast to PartCover you can capture the coverage of 64 Bit assemblies.
PartCover does not support 64 Bit assemblies, and does not work on 64 Bit systems out of the box. You can get it working by using this workaround.
The following table shows which combinations will work:
| OpenCover | PartCover | |
|---|---|---|
| NUnit | 32/64 Bit | 32 Bit |
| MSTest | 32 Bit (MSTest does not support 64 Bit) | 32 Bit |
Advantages of OpenCover
OpenCover is currently released as a beta version, but it works pretty fine already. In my opinion it has the following advantages:
- Supports 64 Bit assemblies
- No manual DLL registration is required
- Lower memory usage
Usage
The provided sample solution contains a simple class and a unit test which can be executed with MSTest and NUnit.
The solution is already compiled in 32 Bit, 64 Bit and AnyCPU mode.
Furthermore the download contains several batch files which enable you to execute the the unit test with OpenCover and PartCover.
Please note: The batch files for MSTest will only work on 64 Bit systems. If you use a 32 Bit OS please adjust the path to MSTest.exe.
OpenCover can be executed in the following way:
OpenCover.Console.exe -register:user -target:nunit-console-x86.exe -targetargs:"/noshadow Test.dll" -filter:+[*]* -output:coverage.xml
PartCover can be executed in the following way:
PartCover.Registration.exe PartCover.dll PartCover.exe --target "nunit-console-x86.exe" --target-args "Test.dll /noshadow" --include [*]* --output coverage.xml PartCover.Registration.exe
After running the unit tests, a XML file containing the coverage information is generated. If you like, you can use ReportGenerator to generate a nice HTML report out of it.
In a previous post I described how you could integrate the code coverage in your build process by using MSBuild.
One final hint: If you want to execute/debug NUnit tests within Visual Studio 2010 you can use this NUnitForVS. This plugin lets you execute the test with the regular test tools:
New comment
Comments
Shaun Wilde
06/22/2011 | http://scubamunki.blogspot.com
PartCover also comes with a registration tool that does the per user registration for those users running under restrictive accounts.Thanks for your effort in supporting OpenCover
Daniel
06/22/2011
@Shaun:Thanks for your comment. I updated my post accordingly.
Alexandre Victoor
06/30/2011
Tanks a lot for this article !No reason anymore not to try opencover :-)
Deedee123
08/09/2011
Thanks for the article!However, does the ReportGenerator display the coverage down to the method level too? I can see only the class coverage %, and clicking on it shows the lines that are covered/not covered.
It will be great to be able to find out which methods are uncovered/less covered at a glance, rather than having to always scroll down to see the lines (esp when classes are huge)
Thanks,
Daniel
08/09/2011
@Deedee123:Currently ReportGenerator only supports coverage by line.
I'm not sure if a method based coverage is really helpful.
If a method has a coverage less that 100%, you have to look at the line coverage to figure out what is missing.
And classes with many many lines of code should be avoided anyway :-)
Deedee123
08/09/2011
OK, yes that is right.Actually my application checks to see if a method has a coverage of more than 90%, if not, it will flag it as an error on the server build. In that case, if there are 8-9 methods in a class and someone is writing tests to cover them, it is helpful to know at a glance which methods still do not meet that criteria (instead of having to scroll down and look for red highlights every time, and calculate the method coverage manually).
This is a common feature in NCoverExplorer, PartCover browser..and so I thought maybe ReportGenerator has it too.
Thanks for you reply.
Malcolm
09/13/2011
I tried out ReportGenerator with OpenCover and they seem to work well.One issue I have found. It doesn't show the coverage for nest/inner classes.
Is there config for this? Or would it be easy to implement?
Daniel
09/13/2011
@Malcolm:Thanks for your comment. I will take a look at the problem.
Marco Medrano
12/13/2011
Good article and the tool helps so much.Thanks a lot, specially by the SampleApplication_VS2010.zip
Damien
03/06/2012
Thanks man, that was really helpful.