Azure devops reportgenerator 任务找不到 coverage.cobertura.xml 文件

Azure devops reportgenerator task can't find coverage.cobertura.xml file

如标题所示,我正在尝试让代码覆盖率在 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'

我的解决方案包含一个 front-end 项目和一个名称以 "Test" 结尾的 xunit 测试项目。

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

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

实际管道的错误是:

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

我尝试在我的电脑上本地生成测试报告,它工作得很好。说明包没问题

我做错了什么?

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

在您的脚本中:

reportgenerator -reports:$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml - ...

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

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

VSTest 集成

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

MSBuild 集成

并且如果您使用 dotnet add package coverlet.msbuildcoverlet.msbuild 包添加到您的项目中。然后您需要以这种方式启用代码覆盖率:dotnet test /p:CollectCoverage=true。而上面的命令是运行之后,会生成一个包含结果的coverage.json文件

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

注:

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

2.Also,检查this document你可以找到:

If you're building on the Windows platform, code coverage metrics can be collected by using the built-in coverage data collector.

If you're building on Linux or macOS, you can use Coverlet or a similar tool to collect code coverage metrics.

所以根据你的脚本内容,更推荐运行在Linux或者macOS代理。如果你只是想让它在 windows 中 运行,它有默认的内置覆盖率数据收集器。

希望以上内容能帮助解决您的困惑和原问题。