OpenCover / opencover

A code coverage tool for .NET 2 and above (WINDOWS OS only), support for 32 and 64 processes with both branch and sequence points
https://blog.many-monkeys.com
Other
1.31k stars 248 forks source link

I only get the coverage of the assembly with the test classes, but not the coverage of the main assembly #720

Closed ComptonAlvaro closed 7 years ago

ComptonAlvaro commented 7 years ago

I have a solution with two projects, one is the main application and the other project is for test the main project. It is because I want to have one assembly for the application and other assembly for the tests, in this way I avoid to include the tests classes in the main assembly because the final assembly that i will release to the clients doesn't need to include the tests classes.

Well, I am trying this command to test my C# application, and the tests are using MSTest. This two commands are in a .bat file:

..\tools\OpenCover.Console.exe -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\MSTest.exe" -targetargs:"/testcontainer:M:\MyAssemblyTest.dll" -output:"M:\coverage results\opencovertests.xml"

M:\software\CMMS\software\CMMS\packages\ReportGenerator.2.5.8\tools\ReportGenerator.exe "-reports:m:\coverage results\opencovertests.xml" "-targetdir:d:\coverage results\reports"

The problem is that in the report that I get I have only the coverage of the assembly MyAssemblyTest.dll, not the MainAssembly.dll that has the methods that MyAssemblyTests.dll is testing. So the coverage is of the test methods, no the main methods.

How I could get the coverage of the methods in my MainAssembly.dll?

Thanks.

sawilde commented 7 years ago

@ComptonAlvaro 1) are you producing PDB files for your main assembly? This is required for instrumentation. 2) are you tests calling anything in your main assembly at the moment? If the code is not loaded it cannot be instrumented and covered. 3) MSTest will copy files about but not their PDBs so you may want to try the /noisolation switch when executing MSTest

sawilde commented 7 years ago

@ComptonAlvaro I didn't get a response - is this issue still outstanding?

sawilde commented 7 years ago

I suspect resolved in #721

sleepyhollo commented 5 years ago

First off, thanks for making and maintaining this! 💯

I am getting this problem. I am using visual studio 2019 (.net framework 4.7.2) in windows 10 with MSTest package.

I have two projects, one for my application (called Project) and another one for test (called tests). I am using a batch a file in the root fo the solution to run the OpenCover. To get this to work, I followed Allen Conway's blog post.

The following is my bat file function to generate the outputs

:RunOpenCoverUnitTestMetrics
"%~dp0packages\OpenCover.4.7.922\tools\OpenCover.Console.exe" ^
-register:user ^
-target:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\mstest.exe" ^
-targetargs:"/testcontainer:\"%~dp0tests\bin\Debug\tests.dll\" /resultsfile:\"%~dp0Project.trx\"" ^
-searchdirs:"%~dp0Project\bin\Debug" ^
-filter:"+[Project*]* -[tests]*" ^
-mergebyhash ^
-skipautoprops ^
-output:"%~dp0\GeneratedReports\ProjectReport.xml"
exit /b %errorlevel%

As you can see I tried using the searchdirs keyword to include my Project bin folder. I also tried using the /noshadow and /noisolation option in targetargs; that did not work. With the above settings, my report consists of a list of system modules which are skipped. If I remove the filter keyword from the call to OpenCover, I only get the test classes in my Report.

sawilde commented 5 years ago

Without seeing the output nor the test structure it is hard to diagnose these things

  1. does the Project assembly have full PDB generated?
  2. does the test assembly execute a test that uses code in the Project assembly? I know this sounds stupid but the tool will only see assemblies that are loaded during runtime.
Snehavj90 commented 4 years ago

Hi i am facing same issue mentioned here, any work around for this.