Failure to send coverage report to Sonarqube in .net Core 3.1

Hi everyone, I hope I didn’t post it in the wrong place.

The libraries I use and their information are as follows

Rider IDE
Net Core 3.1
coverlet.msbuild 3.1.0
Microsoft.CodeCoverage 17.1.0-preview-20211130-02
coverlet.collector 3.1.0

I want the code coverage rate to appear in my .Net Core 3.1 project sonarqube.

I tried many ways, but the result of the report is not visible in the sonarqube portal. Can you help with this?

# 1. cli commands
dotnet sonarscanner begin /k:"Gateway.Authorization.Api" /d:sonar.host.url="http://localhost:9000"  /d:sonar.login="4036de3841c239103c13c2bbc8e62b17c093233f" /d:sonar.cs.opencover.reportsPaths=test/Gateway.Authorization.Api.Test/coverage.opencover.xml /d:sonar.coverage.exclusions="**Test*.cs" /d:sonar.dotnet.excludeTestProjects=true /d:sonar.verbose=true /d:sonar.cs.xunit.reportsPaths=test/Gateway.Authorization.Api.Test/coverage.opencover.xml
dotnet build
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=coverage 
 ./Gateway.Authorization.sln
dotnet sonarscanner end /d:sonar.login="4036de3841c239103c13c2bbc8e62b17c093233f"
# 2. cli commands (other)
dotnet sonarscanner begin /k:”Gateway.Authorization.Api" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="4036de3841c239103c13c2bbc8e62b17c093233f" /d:sonar.coverageReportPaths=“./sonarqubecoverage/SonarQube.xml"

dotnet build
dotnet test ./Gateway.Authorization.sln --no-build --collect:"XPlat Code Coverage"
reportgenerator "-reports:**/Gateway.Authorization.Api.Test/TestResults/*/coverage.cobertura.xml" "-targetdir:sonarqubecoverage" "-reporttypes:SonarQube"
dotnet sonarscanner end /d:sonar.login="4036de3841c239103c13c2bbc8e62b17c093233f"

Screen Shot 2022-01-25 at 10.47.26

Hi,

Welcome to the community!

I’ll start by saying I’m not a .NET expert.

And what I notice is that your default coverage reporting shows 0% coverage in all three columns… just like SonarQube. To me the first interesting question is how you can have a 14.28% coverage with a total of 0%. Seems like a div-by-zero error waiting to blow up.

Do you understand what’s going on with that?

 
Ann

Hello Ann,

First of all, thank you for your interest.
I added a test project to Solution and this is how it comes up in the reports.
Below I share a test result that covers other projects.
I don’t understand the result.


When i added below command;
/p:IncludeTestAssembly=true /p:ExcludeByFile=\"**/Microsoft.NET.Test.Sdk.Program.cs\"

after execute than total coverage changed at terminal;

Screen Shot 2022-01-26 at 14.47.42


But still no results appear in sonarqube portal.

Hi,

Just to be sure, if you click on the 0% in your homepage, it gives you a worst-first list of file coverage. If you scroll to the bottom, does every single file have a 0% next to it?

 
Ann

Hi,
When I click on Coverage 0, the value of all classes on the redirected page is also 0.

I also marked some parts of the projects with [ExcludeFromCodeCoverage] so that the ratio changes. But still the Coverage value is 0.


Last test result:


Last Coverage on portal:

Hi,

Did you add that marking at the file level or just within files to exclude only parts of them?

SonarQube doesn’t read that markup. So if you’ve it to exclude an entire file from your coverage engine, then your coverage report won’t include that file & analysis will proactively count it as 0% covered. (Here’s why.) If you’ve only used it to exclude a subset of the file, then the file will be included in your coverage report and SonarQube go off what the report say.

 
Ann

Hi Ann,
I’ve added the relevant code at the class level.
After this addition, the results in the report have changed.
But when it sends it to sonar, it still shows 0.


When I try .net Core 5.0 in an empty project. After updating some libraries, report flow was provided to Sonar Portal. Is it a version problem?


The following is the content of the current Test project

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <Nullable>enable</Nullable>

        <IsPackable>false</IsPackable>

        <LangVersion>latestmajor</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="coverlet.collector" Version="3.1.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="coverlet.msbuild" Version="3.1.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="FluentAssertions" Version="6.3.0" />
        <PackageReference Include="Microsoft.CodeCoverage" Version="17.0.0" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\..\src\Gateway.Authorization.Api\Gateway.Authorization.Api.csproj" />
    </ItemGroup>

</Project>

Erdoğan

Hi Erdogan,

(Sorry about the missing accent; AmE keyboard :flushed: )

I’m out of my depth at this point, but this post is already tagged to get the more expert attention it needs.

 
Ann

Hi Ann,
Thank you very much for your help. I hope someone else can take care of it.

Hi,
Can anyone help on the subject?

Hello @death_claws

Did you read the code coverage troubleshooting guide for .NET?

Troubleshooting .NET code coverage import