简体   繁体   中英

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 "do.net build" step I included a "do.net 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 "do.net 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM