Search code examples
.netunit-testingsonarqubecode-coveragesonarcloud

How to setup Sonarcloud with code coverage for a .NET project using Coverlet


I'm trying to setup a pipeline that runs sonarscanner, unit tests with code coverage. I can't figure out why it is not working. I'm getting no errors but in sonarcloud is saying 0 errors and 0% coverage.

Coverlet open cover is creating XML file that is actually confirming that there is a percentage covered.

this is my repository: https://github.com/Kwebbel-project/tweet-service/tree/development

This is how my pipeline looks

name: Development

on:
  push:
    branches: [ "development" ]
  pull_request:
    branches: [ "development" ]

jobs:
  Analysis:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '6.0.x'
          
      - name: Install Sonar global tool
        run: dotnet tool install --global dotnet-sonarscanner
        
      - name: Begin sonar scan
        run: | 
          dotnet sonarscanner begin /k:"Kwebbel-project_tweet-service" /o:"kwebbel-project"  /d:sonar.host.url=https://sonarcloud.io /d:sonar.login=${{ secrets.SONAR_TOKEN }} /d:sonar.verbose=true 
        
      - name: Build and run unit tests
        run: |
          dotnet restore
          dotnet build
          dotnet test -p:CollectCoverage=true -p:CoverletOutputFormat=opencover -o="coverage.xml"
          
      - name: End Sonar scan
        run: dotnet sonarscanner end /d:sonar.login=${{ secrets.SONAR_TOKEN }}
        

Solution

  • So I couldn't solve the problem with Sonarcloud so I tried another integration with Codecov. This is how my new Continuous integration pipeline looks.

    name: Continuous integration
    
    on:
     push:
       branches: [ "development", "master" ]
     pull_request:
       branches: [ "development", "master" ]
    
    jobs:
     Analysis:
       runs-on: ubuntu-latest
    
       steps:
         - uses: actions/checkout@v3
         - name: Setup dotnet
           uses: actions/setup-dotnet@v3
           with:
             dotnet-version: '6.0.x'
             
         - name: Restore dependencies
           run: dotnet restore
           
         - name: Run Unit tests and create coverage report
           run: dotnet test tweet-service.csproj /p:CollectCoverage=true /p:IncludeTestAssembly=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile=\"**/Microsoft.NET.Test.Sdk.Program.cs\"
             
         - name: Upload coverage reports to Codecov
           uses: codecov/codecov-action@v3
           with:
             token: ${{ secrets.CODECOV_TOKEN }}
             fail_ci_if_error: true
             files: ./coverage.opencover.xml