.NET Core – calculating code coverage with OpenCover (on Windows)

1. Introduction.

I’ve been struggling for some time to make OpenCover work with .NET Core projects, so I’ve decided to document a working example. This is basically an aggregation of hints and answers I found on the Web (especially in here)

2. Working example.

If you run an OpenCover with the same arguments you were running it for a classic framework, you will end up with something like this
As you can see the coverage was not calculated and OpenCover is complaining about a missing PDBs. In order to fix that we have to do two things. First of all, we have to build the project with a full PDBs

And then run OpenCover with and -oldStyle switch

From now on, OpenCover recognizes PDB files and the code coverage is calculated correctly

3. Cake script.

As we use a Cake as our build automation tool I’ve also prepared a sample script for generating a code coverage. The core part of the script looks as follows

Notice that script is able to merge code coverage results from multiple test projects thanks to MergeOutput flag passed to OpenCover.

Full sources of that script (along with a dotnet vstest alternative approach) can be found here.

.NET Core – calculating code coverage with OpenCover (on Windows)

4 thoughts on “.NET Core – calculating code coverage with OpenCover (on Windows)

  1. Gio says: 

    Thanks for posting.

    where can i find information in generated report:

    how many tests passed, how many executed, how many failed?

    1. TPodolak says: 

      OpenCover does not generate these statistics, it is a role of your task runner. In my case XUnit generates that

  2. hoye says: 

    System.IO.FileNotFoundException: Unable to find tests for G:\Project\Lottery\Program\WEB\Lottery.WEB\bin\Debug\netcoreapp2.0\Lottery.WEB.dll. Make sure test project has a nuget reference of package “Microsoft.NET.Test.Sdk” and framework version settings are appropriate. Rerun with /diag option to diagnose further.
    at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable1 sources,IDictionary2 environmentVariables, TestRunnerConnectionInfo connectionInfo)
    at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, CancellationTokencancellationToken)
    at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyExecutionManager.StartTestRun(TestRunCriteria testRunCriteria, ITestRunEventsHandler eventHandler)

    1. TPodolak says: 

      The error indicates that your Lottery.WEB.dll is not a test project, you probably are trying to run dotnet test for a web project

Comments are closed.