繁体   English   中英

未找到 SonarQube 的 Azure 管道中的报告代码覆盖率

[英]Report Code Coverage in Azure Pipeline for SonarQube not found

我正在努力为 SonarQube 在 Azure DevOps 管道中生成代码覆盖率报告。我需要此代码覆盖率报告,以便 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 管道中生成报告所遵循的步骤:

1- 在“do.net 构建”步骤之后,我包含了一个“do.net 测试”,其中包含以下 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- 在“do.net 测试”步骤之后,我包含了一个 PowerShell 脚本来生成报告并将此报告转换为 Cobertura 格式以用于 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'

据我所知,在 Powershell 这一步之后,我将不得不包括另外 2 个步骤,以将报告发布到 Azure 和 Sonarqube。但是,看起来我在查找单元测试期间生成的报告时遇到了一些问题。

对此有什么想法吗?

谢谢。

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

请参阅此处https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#examples

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

如果未生成 coverage.opencover.xml 文件,请仔细检查您的测试项目中是否有 nuget 对 coverlet.msbuild 和 OpenCover 的引用。 可能只需要 coverlet.msbuild。 您应该能够在 DevOps 的日志中看到这一点,例如:

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

根据我的经验,OpenCover 在 SonarQube 上的表现比 Cobertura 更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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