Dotnet core api unit test coverage is not reflected in sonarqube

  • Version : 6.2.0
  • how is SonarQube: our own server with Docker
  • Reflect unit test coverage in our dotnet api to sonarqube
  • followed bellow documentation and integrated with azure pipeline.
  • .NET test coverage

09:52:18.661 INFO Adding this code coverage report to the cache for later reuse: /home/azureuser/myagent/_work/4/s/coverage.xml
09:52:18.661 DEBUG Analyzing coverage after aggregate found ‘0’ coverage files.
09:52:18.661 DEBUG The total number of file count statistics is ‘0’.
09:52:18.661 INFO Sensor C# Tests Coverage Report Import [csharp] (done) | time=661ms

Hi,

Welcome to the community!

It seems that your coverage reports aren’t available to analysis or aren’t where they’re supposed to be.

Can you provide your pipeline so we can see the order of the steps and the arguments to analysis?

 
Thx,
Ann

1 Like

Hi Ann,

Bellow is our azure pipeline and i have added sensitive word in sensitive information’s.

trigger:
  - develop

resources:
  - repo: self

variables:
  dockerRegistryServiceConnection: "sensitive"
  imageRepository: "sensitive"
  containerRegistry: "sensitive"
  dockerfilePath: "$(Build.SourcesDirectory)/Dockerfile"
  tag: "$(Build.BuildId)"
  vmImageName: "ubuntu-latest"

stages:
  - stage: BuildAndTest
    displayName: Build and Test
    jobs:
      - job: BuildAndTestJob
        displayName: Build and Test Job
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: UseDotNet@2
            displayName: 'Install .NET Core SDK'
            inputs:
              version: 8.x
              performMultiLevelLookup: true
              includePreviewVersions: true

          - task: DotNetCoreCLI@2
            displayName: 'Unit tests'
            inputs:
              command: 'test'
              projects: '**/*Test.csproj'
              arguments: '--collect "XPlat Code Coverage'

          - task: PublishCodeCoverageResults@1
            displayName: 'Publish code coverage'
            inputs:
              codeCoverageTool: 'Cobertura'
              summaryFileLocation: '$(Agent.TempDirectory)/**/*coverage.cobertura.xml'

          - script: |
              dotnet tool install --global coverlet.console
              export PATH="$PATH:~/.dotnet/tools"
            displayName: 'Install Coverlet'

          - script: |
              coverlet ./Test/bin/Debug/net8.0/Test.dll --target "dotnet" --targetargs "test --no-build" --format opencover --output $(Build.SourcesDirectory)/coverage.xml
            displayName: 'Run coverlet for code coverage'

          - script: |
                echo "Checking if coverage file exists..."
                ls -la $(Build.SourcesDirectory)/*.xml
            displayName: 'Check for Coverage File'

          - task: PublishPipelineArtifact@1
            inputs:
              targetPath: '$(Build.SourcesDirectory)'
              artifact: 'build'
              publishLocation: 'pipeline'

  - stage: Sonar
    displayName: SonarQube Analysis Stage
    # dependsOn: BuildAndTest
    jobs:
      - job: Sonar
        displayName: Build the Project for Sonar Analysis
        pool:
          name: sensitive
          demands:
            - Agent.Name sensitive
            - Agent.Version -gtVersion 3.238.0
        steps:

          - task: UseDotNet@2
            displayName: 'Install .NET Core SDK'
            inputs:
              version: 8.x
              performMultiLevelLookup: true
              includePreviewVersions: true

          - task: DownloadPipelineArtifact@2
            inputs:
              artifact: 'build'
              path: '$(Build.SourcesDirectory)'

          - script: |
              echo "Checking if coverage file exists..."
              ls -la $(Build.SourcesDirectory)/*.xml
            displayName: 'Check for Coverage File'

          - task: SonarQubePrepare@6
            displayName: 'Prepare SonarCloud analysis configuration'
            inputs:
              SonarQube: 'sonarqube-devops'
              scannerMode: 'CLI'
              configMode: 'manual'
              cliProjectKey: 'sensitive'
              cliProjectName: 'sensitive'
              cliSources: '.'
              extraProperties: |
                    sonar.verbose=true
                    sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/coverage.xml

          # - task: PublishCodeCoverageResults@1
          #   displayName: 'Publish code coverage'
          #   inputs:
          #     codeCoverageTool: 'Cobertura'
          #     summaryFileLocation: '$(Agent.TempDirectory)/**/*coverage.cobertura.xml'
              
          - task: SonarQubeAnalyze@6
            inputs:
              jdkversion: 'JAVA_HOME_17_X64'

          - task: SonarQubePublish@6
            inputs:
              pollingTimeoutSec: '300'

Hi,

It looks like you’re not doing things in the expected order.

 
HTH,
Ann

Hi Ann,

when I try to build the code in sonar job it fails the build and gives me an error, That’s why I build and test the project before and saved in artifacts. Then in sonar stage I pull those artifacts and pass the report.

Hi,

For .NET, it just doesn’t work right in anything but the documented order. So can you share the error you get?

 
Ann

Hi,

Bellow is the error
##[error]/home/azureuser/myagent/_work/4/.sonarqube/bin/targets/SonarQube.Integration.targets(245,5): Error MSB4018: The “IsTestFileByName” task failed unexpectedly.

Hi,

It’s not clear to me that “IsTestFileByName” is related to analysis.

 
Ann

1 Like

Can you please give me a working sample done by azure yml. it needs to reflect test coverage in sonar cloud.

Hi,

There’s an example in the docs. You’ll want to add in the coverage step after the build.

 
HTH,
Ann