Search code examples
c#.netunit-testingmstestcoverlet

Coverlet does not generate cobertura.xml for MSTest test projects


I have a solution with multiple test projects (MSTest). I wanna check my code coverage for the entire solution. To do this, I type the following command:

dotnet test --collect:"XPlat Code Coverage" "MySolution.sln" --no-build -- DataCollectionRunSettings.DataCollectors.DataCollector.Con
figuration.IncludeDirectory=PathToMyWebApp\bin

In every test project, I see TestResult folders. Each of them contains a subfolder. All the subfolders are empty.

I tried absolutely the same with another one solution. The difference was the solution had xUnit test projects, not MSTest. And I did not have any problem. All the subfolders contained cobertura.xml

I also noticed this error in the console:

Could not find data collector 'XPlat Code Coverage"

I have checked several sources in the internet (including stackoverflow) where this error was mentioned. But I have not found anything working for me. I tried the following advices:

  1. Publish my web project and specify path to the published project. No effect.
  2. Install this <PackageReference Include="coverlet.collector" Version="1.0.1" />. It did not work also, and I completely do not appreciate this approach, because why should I install anything additionally into my code base if my global configuration worked perfectly for other solutions?

Globally I have installed:

coverlet.console 6.0.0
dotnet-reportgenerator-globaltool 5.1.23

Does somebody have any ideas, why it does not want to work with MSTest? By the way, I tried to add an xUnity test project into this solution (that has MSTest projects). And you know what? The folder TestResult had cobertura.xml. So, what is wrong with MSTest? What did I miss?


Solution

  • I was wrong. This thing is compulsory in your .csproj:

    <PackageReference Include="coverlet.collector" Version="3.1.2">
                <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
                <PrivateAssets>all</PrivateAssets>
            </PackageReference>
    

    If you generate a new test project (for example, xUnit project) you will see it added by default.

    I hope this information will save somebody's time.