linuxuserind
linuxuserind

Reputation: 49

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

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,

.\.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.vstest.reportsPaths=**\*.trx /d:sonar.cs.vscoveragexml.reportsPaths=**\*.coveragexml
dotnet build --configuration Release
dotnet test -c release --no-build UnitTest/UnitTest.csproj --collect "Code Coverage" --logger trx
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

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. On checking logs in actions, found the below line which is looking for the converted .coveragexml file where no log message stating convertion is completed.

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

Upvotes: 2

Views: 3296

Answers (1)

linuxuserind
linuxuserind

Reputation: 49

As per SonarCloud blogLink, I used dotnet-coverage tool for Code Coverage my Test project.

We had to install the dotnet-coverage tool before using it inside sonar-scanner.

With the below snippet of yml, I am able to view Coverage in SonarCloud now.

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 }}"

Upvotes: 2

Related Questions