How do I use OpenCover and ReportGenerator to view Unit Test Coverage Results?

atconway picture atconway · May 16, 2013 · Viewed 38.7k times · Source

I'm a noob to using both OpenCover and ReportGenerator and I'm struggling a bit in understanding how to get them working. I'm using VS.NET 2012 'Professional' which means I don't have access to the built in unit test coverage tooling. I also have ReSharper installed, but don't want to pay for another utility in 'dotCover'

It looks like OpenCover and ReportGenerator will do what I need and I see the documentation that was downloaded alongside, but am missing some understanding. 1st off, when I download the nuget packges for both, what should my target project be? I have a multi layer app, so I'm assuming my unit test project correct, or does it even matter? I see in the documentation, I'm just pointing at the /bin (I think) of a solution using command line commands, so maybe I didn't even need to add these downloads to any particular project (could have been a test harness). Can someone tell me if I have this correct?

Once I have them installed, I'm trying to get unit test coverage metrics, and the docs that came with the package are not as clear as I hoped. Are there any good blog posts or links that walk through using these tool together to get the metrics?

Answer

SonalKhodiyar picture SonalKhodiyar · May 17, 2013

you do not need to add these to particular project

I use report generator and open cover to generate test coverage results too. This is the script I use to generate the codecoverage using opencover

"C:\Program Files (x86)\OpenCover\OpenCover.Console.exe" -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" -targetargs:"/noisolation /testcontainer:\"C:\\bin\Debug\.dll\" /resultsfile:C:\Reports\MSTest\.trx" -filter:"+[]" -mergebyhash -output:C:\Reports\MSTest\projectCoverageReport.xml

Note that if your argument needs to escape quotes i.e. to pass arguments with spaces to that target process then you can use ", e.g.: -targetargs:"c:\program files"

This is the script I use to run report generator.

C:\ReportGenerator\bin\ReportGenerator.exe -reports:"C:\Reports\MSTest\projectCoverageReport.xml" -targetdir:"C:\Reports\CodeCoverage"

Hope this helps.