Code coverage report for .Net not working on Linux agent

For our build pipeline in Azure DevOps we have switched to using the ubuntu-latest vm-image and since that switch I have noticed we no longer receive code coverage information. I have verified that the problem is due to using a Linux agent by switching back to windows and see that it worked, however due to a problem with the current Windows agents we have to use the Linux agent. Is it possible there is a problem with the default path that sonar checks for the coverage information? I don’t see any problems in the attached log. sonarlog2.txt (296.2 KB)

Below the pipeline:


pool:
  vmImage: 'ubuntu-latest'

variables:
  - name: solution
    value: '**/*.sln'
  - name: buildPlatform
    value: 'Any CPU'
  - name: buildConfiguration
    value: 'Test'

steps:

  - task: DotNetCoreCLI@2
    displayName: 'Restore solution'
    inputs:
      command: 'restore'
      projects: '**/*.sln'
      feedsToUse: 'select'

  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis configuration'
    inputs:
      SonarCloud: 'SC'
      organization: 'org'
      scannerMode: 'MSBuild'
      projectKey: 'key'
      projectName: 'name'

  - task: DotNetCoreCLI@2
    displayName: 'Build solution'
    inputs:
      command: 'build'
      projects: '**/*.sln'
      arguments: --configuration $(BuildConfiguration)

  - task: DotNetCoreCLI@2
    displayName: 'Execute Unit tests'
    inputs:
      command: 'test'
      projects: '**/*.Tests.csproj'
      arguments: '--collect "Code Coverage"'
      
  - task: SonarCloudAnalyze@1
    displayName: 'Run SonarCloud analysis'


  - task: SonarCloudPublish@1
    displayName: 'Publish results on build summary'
    inputs:
      pollingTimeoutSec: '300'
               
1 Like

Hi Dave

As you have discovered the automatic collection of coverage is only supported on Windows. Microsoft have created a new tool that is cross-platform called dotnet-coverage and we have updated the documentation on how to set it up here

Hope that helps

Tom

I have an issue with this when I want to run it on linux agent. In the example .NET Test Coverage | SonarCloud Docs provided by sonar cloud the dotnet test job arguments: '--collect "Code Coverage"' will not work in linux that only works in windows.

Hi! Since version 17.5 someone claims that MS’ dotnet test --collect “Code Coverage” is now fully cross platform:

Is that something Sonar is looking into?

So, apparently this community (or at least this topic) is completely dead. No-one else interested in code coverage on Linux?

2 Likes

+1 to this, we currently have to have 2 separate jobs to build our code twice, once on windows, once in linux due to SonarQube’s lack of support for linux. Would be great to see this fixed.

Are there any updates on this? I tested today with

pool:
  vmImage: ubuntu-latest

and

- script: dotnet test MyLibrary-Tests/MyLibrary-Tests.csproj --collect "Code Coverage"
  displayName: 'dotnet test MyLibrary-Tests'

and no coverage gets reported

1 Like

Tom_Howlett (new users cannot mention, grrrrr) are there any news about this?

I think it is interesting to have code coverage under linux agents as it is with windows agents

Can anybody mention Tom??

TY

@Tom_Howlett any news about this working in Linux agents?

@Cesar_Diaz I have got it working by now using OpenCover.

In the sonar prepare step I have added the following:

extraProperties: |
       sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/*/coverage.opencover.xml

Then the test step I have defined as follows:

- task: DotNetCoreCLI@2
    displayName: 'Unit tests'
    inputs:
      command: 'test'
      projects: '**/*.UnitTests.csproj'
      arguments: '--no-build --configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'

Note the two formats, Cobertura is used to create a code coverage report in DevOps, and Opencover to create a report for SonarCloud

To get the Cobertura report in DevOps I added an additional step to publish the report, this is not needed for Sonar:

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

Nice @DdeM

I have it working now

TY

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