SonarCloud Code Coverage always coming as 0 - GitHub Actions dotnet Core

  • ALM used (GitHub)
  • CI system used (GitHub Actions)
  • Languages of the repository(.NET Core 3.1)

I am trying to get the Code Analysis done with SonarCloud using the Github actions for my .NET Core application. I have added the below code in my build.yml file for .NET Core Build & Test to check if Code Coverage is populating in SonarCloud.io,

 dotnet build --configuration Release
 dotnet test -c release --no-build aspnet-core-dotnet-core.UnitTests/aspnet-core-dotnet-core.UnitTests.csproj --collect:"Code Coverage"

In the GitHub Actions log able to see the below line

D:\a\DevOpsStarterMvc\DevOpsStarterMvc\Application\aspnet-core-dotnet-core.UnitTests\TestResults\4078eefd-c66e-4ee4-a847-b322df1f407f\runneradmin_fv-az450-602_2022-03-23.10_39_01.coverage

Which is generating .coverage file and the github run on windows-latest instance. But I am seeing 0.0% for the Coverage in the SonarCloud’s Project Summary.

Anything I am missing here to get the value for the CodeCoverage.

P.S The same thing happened in the Azure DevOps Pipeline implementation with SonarCloud extensions. Its showing 0.0% for the Coverage.
Test & ScanEnd Logs.txt (21.0 KB)
build.yml.txt (2.1 KB)

In Logs I am seeing this message

WARN: Could not find any coverage report file matching the pattern ‘$(Agent.TempDirectory)***.coveragexml’.

Only converage file it created with “CodeCoverage” but converagexml file is not generated as per log messages.

Hi, welcome to the community

We’ve recently updated the documentation on .NET coverage in SonarCloud and hopefully this is clearer, had you seen it?

When using the dotnet tool the coverage file is not automatically converted to xml as it can do with the .NET Framework version of the scanner. Microsoft have recently released an alternative tool that can generate the xml in the correct format called the dotnet-coverage tool, in this scenario I would recommend you try this.

Please let me know how you get on

Tom

2 Likes

I will update the actions and let u know… appreciate the suggestion.

With dotnet-coverage tool I am able to get the coverage data in sonarcloud project

name: Build
on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build
    runs-on: windows-latest
    steps:
      - name: Build
        run: |
          dotnet tool install --global dotnet-coverage

          [jdk setup]

          .\.sonar\scanner\dotnet-sonarscanner begin /k:"projectkey" /o:"organization" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
          dotnet build --configuration Release
          dotnet-coverage collect 'dotnet test' -f xml -o 'coverage.xml'
          .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
3 Likes

That’s great, thanks for letting us know :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.