Azure devops 报告生成器任务找不到coverage.cobertura.xml 文件

Lor*_*ris 8 code-coverage azure-devops

正如标题所示,我正在尝试让代码覆盖率在 Azure Devops Pipeline 上运行。

这是管道:

trigger:
 - master

pool:
   vmImage: 'windows-latest'

variables:
   solution: '**/*.sln'
   buildPlatform: 'Any CPU'
   buildConfiguration: 'Release'

steps:
- task: DotNetCoreInstaller@0
  displayName: 'Installing .NET Core SDK...'
  inputs:
    version: 3.1.101

- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'Building $(buildConfiguration)...'

- task: DotNetCoreCLI@2
  displayName: 'Executing dotnet tests...'
  inputs:
    command: test
    arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\Coverage\'
    projects: '**/*Test/*.csproj'
    nobuild: true

- script: |
  dotnet tool install -g dotnet-reportgenerator-globaltool
  reportgenerator -reports:$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml - targetdir:$(Build.SourcesDirectory)\CodeCoverage -reporttypes:HtmlInline_AzurePipelines;Cobertura
  displayName: 'Creating code coverage report...'

- task: PublishCodeCoverageResults@1
  displayName: 'Publishing code coverage...'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.SourcesDirectory)\CodeCoverage\Cobertura.xml'
    reportDirectory: '$(Build.SourcesDirectory)\CodeCoverage'
Run Code Online (Sandbox Code Playgroud)

我的解决方案包含一个前端项目和一个名称以“Test”结尾的 xunit 测试项目。

测试运行得很好,但我总是在这一行收到错误:

报告生成器-报告:$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml ...

实际管道的错误是:

报告文件“D:\a\1\s\TestResults\Coverage\coverage.cobertura.xml”无效。文件不存在(完整路径:'D:\a\1\s\TestResults\Coverage\coverage.cobertura.xml')。

我尝试在我的电脑上本地生成测试报告,效果很好。这意味着包没问题。

我做错了什么?

Lan*_*SFT 12

正如错误消息所示,它需要coverage.cobertura.xml但未找到。(未生成!)

在你的脚本中:

reportgenerator -reports:$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml - ...
Run Code Online (Sandbox Code Playgroud)

预期的输入文件coverage.cobertura.xml但未生成。根据我的经验,这是因为您的dotnet test任务没有成功生成此文件。

我相信您的dotnet test任务可能会成功,没有任何错误,但如果我们检查日志,我们会发现它没有生成预期的coverage.cobertura.xml文件。问题原因可以参考这个文档

VS测试集成

如果使用dotnet add package coverlet.collector命令将coverlet.collector包添加到项目中,则应该使用命令dotnet test --collect:"XPlat Code Coverage"生成coverage.cobertura.xml包含结果的文件。

MSBuild集成

如果您coverlet.msbuild使用dotnet add package coverlet.msbuild. 然后,您需要通过以下方式启用代码覆盖率:dotnet test /p:CollectCoverage=true。运行上述命令后,coverage.json将生成一个包含结果的文件。

由于 dotnet test 命令下面的脚本需要一个coverage.cobertura.xml文件,因此您应该使用VSTest Integration,这意味着您应该使用 commanddotnet test --collect:"XPlat Code Coverage"而不是dotnet test /p:CollectCoverage=true. 这对于生成丢失的文件是有意义的。

笔记:

1.在我的测试中,dotnet test尽管没有生成代码覆盖率报告,但任务不会抛出任何错误。我们可以随时检查任务日志以获取我们需要的真实信息。

2.此外,检查此文档您可以找到:

如果您在 Windows 平台上进行构建,则可以使用内置的覆盖率数据收集器来收集代码覆盖率指标。

如果您在 Linux 或 macOS 上进行构建,则可以使用 Coverlet 或类似工具来收集代码覆盖率指标。

因此,根据您的脚本内容,更建议在Linux或macOS代理中运行。如果您只是希望它在 Windows 中运行,它有默认的内置覆盖率数据收集器。

希望以上内容有助于解决您的困惑和原始问题。

  • 非常感谢。我发现了这个问题。我只是忘记推送包“coverlet.msbuild”。现在我发现我不能使用所有这些东西在拉取请求中使用代码覆盖分支策略。它仅支持 .coverage 文件。 (2认同)