Search code examples
c#code-coveragemstestopencover

When I run the coverage tests, I get the coverage of the code of the tests methods, no the coverage of the methods that I want to test


I have a project with classes and methods that I want to test. I have another project with the test methods that will test the methods of my main project.

I run the tests with opencover and I generate the reports with reportgenerator, with this commands that I have in a .bet file:

..\tools\OpenCover.Console.exe -register:user -target:"C:\myDllWithTests.dll" -output:"c:\coverage\opencovertests.xml"

.\ReportGenerator.exe "-reports:c:\coverage\opencovertests.xml" "-targetdir:c:\coverage\opencovertests.xml\reports"

I am using MSTest for testing.

The problem is that in the html report, I see that the code that is covered is the tests methods, not the methods in my test main project.

How I could add the main methods in the result?

Thanks.


Solution

  • In target argument for OpenCover pass the path to MSTest (e.g. "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe") and specify your test assemblies (e.g. "C:\myDllWithTests.dll") in targetargs argument.

    To remove test assemblies from code coverage statistics, specify them in filter argument.

    Below is OpenCover command that works fine for me. Here code under test is placed in SampleApp.dll and test code is placed in SampleApp.Tests.dll.

    .\OpenCover.Console.exe -register:user -mergebyhash -target:"c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe" -targetargs:"/testcontainer:\"d:\test\SampleApp\SampleApp.Tests\bin\Debug\SampleApp.Tests.dll\"" -output:UTResults.xml -filter:"+[SampleApp*]* -[SampleApp.Tests]*"
    

    Result report contains only stats for SampleApp.dll assembly, SampleApp.Tests.dll is excluded:

    enter image description here

    Check this answer for some more details. There is also a great article by Allen Conway on using OpenCover & ReportGenerator for .Net projects.