Álvaro García
Álvaro García

Reputation: 19396

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.

Upvotes: 2

Views: 1952

Answers (2)

Mike Upjohn
Mike Upjohn

Reputation: 1297

This might be quite a late answer here, but I've spent an hour or two playing with this and found the following will fix this. It's worth noting that I had the original bat script from another project that I know works, and just changed the DLL file name, so I know the script was OK.

The additional check to make is to:-

  1. Right click the project that has the source code you want visible in the coverage report (not the unit test project) and click Properties
  2. Select Build > Output > Advanced
  3. Set Debugging information to Full
  4. Rebuild solution and re-run bat file.

Works for me in Visual Studio 2019 with .NET Framework 4.7.2 projects.

Upvotes: 0

CodeFuller
CodeFuller

Reputation: 31312

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.

Upvotes: 2

Related Questions