4

I created a test unit for a class and it passed well on my local, on sonarqube it is shown as 0% for Coverage, I found post advice to add coverlet.msbuild I added but still no news:

   <PackageReference Include="xunit" Version="2.4.1" />
   <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
   <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
   <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
   <PackageReference Include="coverlet.msbuild" Version="3.0.3">
     <PrivateAssets>all</PrivateAssets>
     <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> 
   </PackageReference>
 </ItemGroup>

Any idea?

Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
Sacamoto
  • 107
  • 1
  • 8
  • With Coverlet.MsBuild, you need to add `/p:CollectCoverage=true` when executing tests or added as a Property in your project file `true` however there are also various other settings you need to tweak (such as output format and location) as well as some sonar settings... all for Sonarqube to pick it up and recognize the data in the file (even more settings if you are running on linux) – pinkfloydx33 Apr 04 '21 at 21:28
  • How did you collect the coverage? – Pavel Anikhouski Apr 05 '21 at 09:25

2 Answers2

1

Try adding the following to your .csproj

<PropertyGroup>
  <DebugType>Full</DebugType>
</PropertyGroup>

<PackageReference Include="Microsoft.CodeCoverage" Version="16.9.4" />
thepirat000
  • 12,362
  • 4
  • 46
  • 72
1

Can you try coverlet.collector instead of coverlet.msbuild? I have personally used coverlet collector several times successfully.

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

According to this GitHub issue.

Collectors are pretty new(last arrived) and are the best way to do coverage because are strictly integrated with vstest platform(dotnet test) and due to that is the default choice for every .NET core xunit project by Microsoft. So if you create xunit project Microsoft xunit template inject by design our coverage engine as first class coverage tool running with dotnet test --collect:"XPlat Code Coverage"

It may be possible that you are running into this known issue.

Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53