'Report Code Coverage in Azure Pipeline for SonarQube not found

I'm struggling to generate a code coverage repot in Azure DevOps Pipeline for SonarQube. I need this Code Coverage report so SonarQube will be aware about total code coveraged in the Unit Tests.

I am getting this error message in the step to get the report created during the Unit tests:

2022-01-20T15:56:14.9054419Z ========================== Starting Command Output ===========================
2022-01-20T15:56:14.9976248Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\Agents\xx\_work\_temp\cd6ffda1-xx-799dfd098791.ps1'"
2022-01-20T15:56:18.7184739Z You can invoke the tool using the following command: reportgenerator
2022-01-20T15:56:18.7185160Z Tool 'dotnet-reportgenerator-globaltool' (version '5.0.2') was successfully installed.
2022-01-20T15:56:18.9728580Z 2022-01-20T07:56:18: Arguments
2022-01-20T15:56:18.9729027Z 2022-01-20T07:56:18:  -reports:D:\Agents\xxx\_work\26\s/**/TestResults/coverage.opencover.xml
2022-01-20T15:56:18.9729118Z 2022-01-20T07:56:18:  -targetdir:coverage/Cobertura
2022-01-20T15:56:18.9729175Z 2022-01-20T07:56:18:  -reporttypes:Cobertura;HTMLInline;HTMLChart
2022-01-20T15:56:19.1740700Z 2022-01-20T07:56:19: The report file pattern 'D:\Agents\xxx\_work\26\s/**/TestResults/coverage.opencover.xml' found no matching files.
2022-01-20T15:56:19.1741530Z 2022-01-20T07:56:19: No report files specified.
2022-01-20T15:56:19.8181717Z ##[error]PowerShell exited with code '1'.
2022-01-20T15:56:19.8683861Z ##[section]Finishing: Generate Coverage Report

Steps that I followed to generate the report in Azure Pipeline:

1- After a "dotnet build" step I included a "dotnet test" with the following arguments:

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: test
    projects: '**/UnitTests.csproj'
    arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./TestResults/'
  continueOnError: true

2- After "dotnet test" step I included a PowerShell script to get the report generated and Convert this report into Cobertura format for Azure DevOps:

steps:
- powershell: |
   dotnet tool install dotnet-reportgenerator-globaltool --tool-path .
   ./reportgenerator "-reports:$(Build.SourcesDirectory)/**/TestResults/coverage.opencover.xml" "-targetdir:coverage/Cobertura" "-reporttypes:Cobertura;HTMLInline;HTMLChart"
  displayName: 'Generate Coverage Report'

As far as I know after this Powershell step I will have to include 2 more steps to publish the reports into Azure and Sonarqube. However, it looks like I have some issues to find the report generated during the Unit Tests.

Any idea about this?

Thanks.



Solution 1:[1]

If you add coverlet coverage nuget package in your test project then you do not need to use opencover intermediate step at all. The gives you output in cobertura format, which can be published by code coverage yaml task

see here https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#examples

and

here https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=linux

Example:

Below steps execute tests and publish test results and code coverage to Azure DevOps. If you want code coverage results to be published to SonarQube server then use the file ./TestResults/**/coveragereport/Cobertura.xml and publish using PowerShll/Script task.

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: test
    projects: '**/UnitTests.csproj'
    arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -r ./TestTesults --logger trx --logger "console;verbosity=detailed"'
  continueOnError: true
- task: PublishTestResults@2
  displayName: Publishing Test Results
  condition: always()
  inputs:
    testResultsFormat: "VSTest"
    testResultsFiles: "./TestResults/**/*.trx"
    testRunTitle: "Unittests"
    failTaskOnFailedTests: true
- task: PublishCodeCoverageResults@1
  displayName: Publish Code Coverage Results
  condition: always()
  inputs:
    codeCoverageTool: "Cobertura"
    summaryFileLocation: "./TestResults/**/coverage.cobertura.xml"
    pathToSources: [Your code location]
    reportDirectory: ./TestResults/**/coveragereport/coveragereport
   

Hope this helps

Solution 2:[2]

If the coverage.opencover.xml file is not being generated, double check if you have a nuget references on your test project to coverlet.msbuild and OpenCover. Probably only coverlet.msbuild is required. You should be able to see this in the logs on DevOps eg:

Calculating coverage result...
  Generating report 'D:\a\1\s\coverage\coverage.opencover.xml'

From my experience, OpenCover played better with SonarQube than Cobertura did.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Juan.M