Search code examples
code-coveragesonarcloud

Sonarcloud won't show code coverage (.NET project and Coverlet)


I've got a .NET web application and would like the code coverage (calculated with Coverlet) to be shown in Sonarcloud, triggered by GithubAction. Here's the relevant snippet:

sonar-analysis:
    #name: Build and analyze
    runs-on: ubuntu-latest
    steps:
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'zulu' # Alternative distribution options are available.
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 5.0.x
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: Cache SonarCloud packages
        uses: actions/cache@v3
        with:
          path: ~/sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache SonarCloud scanner
        id: cache-sonar-scanner
        uses: actions/cache@v3
        with:
          path: ./.sonar/scanner
          key: ${{ runner.os }}-sonar-scanner
          restore-keys: ${{ runner.os }}-sonar-scanner
      - name: Install SonarCloud scanner
        if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
        run: |
          mkdir -p ./.sonar/scanner
          dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
      - name: Install Coverlet
        run: |
          dotnet tool install --global coverlet.console
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: |
          ./.sonar/scanner/dotnet-sonarscanner begin /k:"<...>" /o:"<...>" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths=coverage.opencover.xml
          dotnet build --no-incremental
          coverlet <path to Tests.dll> --target "dotnet" --targetargs "test --no-build" -f=opencover
          ./.sonar/scanner/dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

Problem: Sonarcloud just won't show the code coverage, although:

  1. the file coverage.opencover.xml has been created in Github Action (it says Calculating coverage result... Generating report '/home/runner/work/sqs/sqs/coverage.opencover.xml')
  2. the coverage data generated by Coverlet is clearly shown in the Github Action console

I tried all the official tutorials and Sonarcloud troubleshooting guides, but nothing seems to work. Thanks a lot for your help


Solution

  • I did it using:

    dotnet-coverage collect "dotnet test --results-directory --logger trx" -f xml -o file.xml

    And it worked. But you also need to pass this setting to Sonar Cloud Prepare:

    sonar.cs.vscoveragexml.reportsPaths=**/file.xml

    I tried to used coverlet but it was not working, so I used different tool.