.net codecoverage not uploaded to SonarQube

Done that.
Still no coverage transferred to SonarQube.

Hi @Simon.Greter,

I think the problem comes from the fact that you are using the .NET Core version of the Scanner for .NET, and that doesn’t support the ā€œVisual Studio code coverageā€ tool.

The automatic discovery of the coverage output generated by the --collect "Code Coverage" parameter only works in the .NET Framework of the scanner.

The documentation that you have used mentions that. However, it doesn’t put any emphasis on the requirement, which is indeed easy to miss.

In these cases, the .NET Framework scanner will automatically find the coverage output generated …

The main documentation page of the Scanner for .NET on SonarQube has a note more explicitely mentioning the limitation:

The .NET Core version of the scanner does not support TFS XAML builds and automatic finding/conversion of Code Coverage files.

The second problem is that you are running your pipeline on Ubuntu (vmImageName: 'ubuntu-latest'). Therefore, you cannot simply switch to the .NET Framework of the scanner.

That is why the documentation states that Visual Studio code coverage is recommended only with an Azure DevOps Windows image: because automatic discovery of coverage files is currently only supported on a Windows machine.

I will need to check internally why the Visual Studio code coverage tool is only supported on Windows (as shown here), via the .NET Framework scanner.

For the time being, I am afraid you will have to:

  • either use a different method of generating the coverage: dotnet-coverage (Visual Studio independent, .NET Core version of the code coverage tool by Microsoft), dotCover, etc.

  • or switch to a Windows image, to be able to run the .NET Framework version of the scanner

I will also take as input the need to clarify the documentation of this section, to more clearly state the requirement.

Hope it helps,
Antonio

1 Like

Hi Antonio

Thanks for explanation.
It would be good if this instructions would also show to achieve the same on linux:
.NET test coverage (sonarsource.com)

Do you have a example on how to configure coverage in a pipeline that runs under linux?

Regards,

Simon

Hi @Simon.Greter,

Yes, we have created an item in our backlog to investigate whether we can extend the ā€œVisual Studio Code Coverageā€ functionality to also run under Linux.

Historically, the automatic import of the result of the coverage relied on the CodeCoverage.exe tool by Microsoft, only available in Visual Studio Enterprise, on Windows.

The tool, however, has been deprecated by Microsoft, and version 6.0 of the Scanner for .NET has removed this dependency, parsing the coverage report produced by Visual Studio in a different way. So we may be able to support coverage import on Linux in the future.

Back to the current state of things, the only option you currently have on Linux is to:

  1. pick an alternative to Visual Studio Code Coverage, e.g. dotnet-coverage

  2. add that step explicitly to your Azure DevOps pipeline, e.g. for dotnet-coverage you may add a step to your build pipeline like the following (after the build step and before the ā€œscanner endā€ step):

- task: PowerShell@2
    displayName: 'dotnet-coverage collect'
    name: dotnet-coverage-collect
    inputs:
      targetType: 'inline'
      script: |
        dotnet-coverage collect 'dotnet test' -f xml  -o 'coverage.xml'
      pwsh: true
  1. Edit the begin step to include the appropriate reportsPath variable: e.g. for dotnet-coverage you need to add sonar.cs.vscoveragexml.reportsPaths to point to coverage.xml, so you would add an item to extraProperties of SonarQubePrepare@5 like the following:
- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'SonarQubeLoyaltyAPI'
    scannerMode: 'MSBuild'
    projectKey: 'XXXXXXX'
    projectName: 'XYZ'
    extraProperties: |
      sonar.branch.name=$(Build.SourceBranchName)
      sonar.cs.vscoveragexml.reportsPaths=coverage.xml

Hope it helps,
Antonio

That worked :slight_smile:
Thanks for your help.