2

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

2 Answers2

2

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.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • I tried adding the targetargs, but the result is the same. About the article, I knew it, but he didn't offer the source code, and I have the doubt if the tests are in the same dll than the main code or is anothe different dll. Beucause in my case the problem is that I only get the coverage of the test assembly and not of the main assembly. – Álvaro García May 28 '17 at 09:03
  • I have tried with this filter: -filter:"+[\*]\* -[\*Tests]\*" -mergebyhash but it doesn't include the main.dll that is in the same folder than the test.dll. Also I have tried with the complete name of the main dll and still it doesn't work. – Álvaro García May 28 '17 at 11:11
  • May be the problem is not with OpenCover, but with the tests itself. What is the output of MSTest? Do you see stats on executed tests, e.g. "1/1 test(s) Passed"? – CodeFuller May 28 '17 at 11:45
  • Yes, I can see the name of the tests that are run and the number that are passed and the number that are not passed. – Álvaro García May 28 '17 at 14:37
  • I am thinking, opencover is compatible with .net standard? – Álvaro García May 28 '17 at 15:18
  • I am sure that opencover is not compatible with .net standard yet. I have try the same with a .net framework project and it works as expected. – Álvaro García May 28 '17 at 15:55
0

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.

Mike Upjohn
  • 1,251
  • 2
  • 16
  • 38