azure - 在 Azure Pipeline 中找不到 SonarQube 的报告代码覆盖率

标签 azure azure-devops sonarqube azure-pipelines code-coverage

我正在努力在 Azure DevOps Pipeline 中为 SonarQube 生成代码覆盖率报告。我需要此代码覆盖率报告,以便 SonarQube 能够了解单元测试中的总代码覆盖率。

我在单元测试期间创建报告的步骤中收到此错误消息:

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

我在 Azure Pipeline 中生成报告所遵循的步骤:

1- 在“dotnet 构建”步骤之后,我包含了一个带有以下参数的“dotnet 测试”:

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- 在“dotnet test”步骤之后,我添加了一个 PowerShell 脚本来生成报告,并将该报告转换为用于 Azure DevOps 的 Cobertura 格式:

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'

据我所知,在执行此 Powershell 步骤之后,我还必须执行 2 个步骤才能将报告发布到 Azure 和 Sonarqube。但是,我在查找单元测试期间生成的报告时遇到了一些问题。

对此有什么想法吗?

谢谢。

最佳答案

如果您在测试项目中添加 coverlet 覆盖 nuget 包,那么您根本不需要使用 opencover 中间步骤。为您提供 cobertura 格式的输出,可以通过代码覆盖 yaml 任务发布

看这里 https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#examples

这里 https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=linux

示例:

以下步骤执行测试并将测试结果和代码覆盖率发布到 Azure DevOps。如果您希望将代码覆盖率结果发布到 SonarQube 服务器,请使用文件 ./TestResults/**/coveragereport/Cobertura.xml 并使用 PowerShll/Script 任务发布。

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
   

希望这有帮助

关于azure - 在 Azure Pipeline 中找不到 SonarQube 的报告代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70801899/

相关文章:

c# - ASP.NET:发布网站不发布资源文件夹

Azure 存储浏览客户端软件选项

xamarin - 如何在没有 Mac 的情况下构建 iPhone Xamarin 应用程序?

azure-devops - 无法扩展变量 '...' 。检测到循环引用

mysql - 连接到 MySQL 时 SonarQube 未启动

SonarQube Build Breaker 插件 : Report processing did not complete successfully: FAILED

java - SonarQube +单元测试覆盖率+Java项目

azure - Azure cmdlet 之间的差异 - New-WAPackWebsite 和 New-AzureWebsite

Azure 应用服务通过自动缩放部署到多个实例

azure - 使用 Powershell 删除 Azure DevOps 上的分支