Azure devops reportgenerator任务找不到coverage.cobertura.xml文件 [英] Azure devops reportgenerator task can't find coverage.cobertura.xml file

查看:104
本文介绍了Azure devops reportgenerator任务找不到coverage.cobertura.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我正在尝试使代码覆盖范围适用于Azure Devops Pipeline.

as the title suggests, i'm trying to get the code coverage to work on 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'

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

My solution contains a front-end project and a xunit test project with the name ending in "Test".

测试运行正常,但是我总是在此行出现错误:

Tests are running just fine but i always get an error at this line:

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

在实际管道中,错误是:

With the actual pipeline the error is:

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

The report file 'D:\a\1\s\TestResults\Coverage\coverage.cobertura.xml' is invalid. File does not exist (Full path: 'D:\a\1\s\TestResults\Coverage\coverage.cobertura.xml').

我试图在我的PC上本地生成测试报告,但效果很好.这表示包装还可以.

I tried to generate test reports locally on my pc and it works just fine. It means that the packages are ok.

我做错了什么?

推荐答案

就像错误消息所指示的那样,它需要 coverage.cobertura.xml ,但是找不到.(未生成!)

Just as the error message indicates, it needs the coverage.cobertura.xml but it's not found.(not generated!)

在您的脚本中:

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

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

The coverage.cobertura.xml is expected input file but it's not generated. In my experience, it's because your dotnet test task didn't generate this file successfully.

我相信您的 dotnet测试任务可能会成功完成而没有任何错误,但是如果我们检查日志,我们会发现它未生成预期的 coverage.cobertura.xml 文件.对于问题的原因,您可以参考本文档:

I believe your dotnet test task may succeed without any error, but if we check the log we can find it didn't generate the expected coverage.cobertura.xml file. For the cause of issue you can refer to this document:

VSTest集成

如果使用 dotnet add package coverlet.collector 命令向项目添加 coverlet.collector 软件包,则应使用命令 dotnet test --collect:"XPlat代码覆盖率" 生成包含结果的 coverage.cobertura.xml 文件.

If you use dotnet add package coverlet.collector command to add coverlet.collector package to your project, you should use command dotnet test --collect:"XPlat Code Coverage" to generate a coverage.cobertura.xml file containing the results.

MSBuild集成

如果使用 dotnet将 coverlet.msbuild 包添加到项目中,则添加包coverlet.msbuild .然后,您需要以这种方式启用代码覆盖: dotnet test/p:CollectCoverage = true .运行上述命令后,将生成一个包含结果的 coverage.json 文件.

And if you add coverlet.msbuild package to your project using dotnet add package coverlet.msbuild. You then need to enable code coverage in this way: dotnet test /p:CollectCoverage=true. And after the above command is run, a coverage.json file containing the results will be generated.

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

Since your script below dotnet test command wants a coverage.cobertura.xml file, you should use VSTest Integration, and it means you should use command dotnet test --collect:"XPlat Code Coverage" instead of dotnet test /p:CollectCoverage=true. That makes sense to generate the missing file.

注意:

1.在我的测试中,尽管未生成代码覆盖率报告,但 dotnet test 任务不会引发任何错误.我们始终可以在任务日志中查看所需的真实信息.

1.In my test, dotnet test task won't throw any error though the code-coverage report is not generated. We can always check the task log for real info we need.

2.此外,请检查

2.Also, check this document you can find:

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

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

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

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

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

So according to the content of your script, it's more recommended to run in Linux or macOS agents. If you're just wanting it to run in windows, it has default built-in coverage data collector.

希望以上所有内容都能解决您的难题和原始问题.

Hope all above helps to resolve your puzzle and original issue.

这篇关于Azure devops reportgenerator任务找不到coverage.cobertura.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆