发布管道 Azure Devops 代码覆盖率报告 [英] Publish a pipeline Azure Devops code coverage report
问题描述
我正在尝试在我的 Azure DevOps Pipeline 中在线发布一份详细报告,但我得到的只是一个下载此 Coverage 文件的链接.(自 Visual Studio 2019 起无法再使用社区版本读取)
I am trying to publish a detailed report online in my Azure DevOps Pipeline, but all I got is a link to download this Coverage file. (That can not be read anymore with the community version since the Visual Studio 2019)
这是我的管道:
trigger:
branches:
include:
- '*'
pool:
vmImage: 'windows-2019'
steps:
- task: NuGetToolInstaller@0
displayName: Instal Nuget
inputs:
checkLatest: true
- task: NuGetCommand@2
displayName: Restore Nuget Packages
inputs:
restoreSolution: '**/*.sln'
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
version: 3.1.x
performMultiLevelLookup: true
- task: DotNetCoreCLI@2
displayName: Build Tests
inputs:
command: 'build'
projects: '**/OneTienditaUnitTests/*.csproj'
arguments: '--configuration Release'
- script: dotnet test OneTienditaUnitTests --logger trx --collect "Code coverage"
- task: PublishTestResults@2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
- task: XamarinAndroid@1
displayName: Build Android App
inputs:
projectFile: '**/*Android*.csproj'
outputDirectory: '$(build.binariesDirectory)/Release'
configuration: 'Release'
如果我像这样使用 Cobertura,则不起作用:
and if I use Cobertura like this, doesn't work:
- task: DotNetCoreCLI@2
displayName: Run Tests
inputs:
command: 'test'
projects: '**/OneTienditaUnitTests/*.csproj'
arguments: '--configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=../reports/coverage/'
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage results'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(build.sourcesdirectory)
eportscoveragecoverage.cobertura.xml'
reportDirectory: '$(build.sourcesdirectory)
eportscoverage'
请问有什么帮助吗?我不是专业的 DevOps
Please any help? I am not a professional DevOps
推荐答案
要发布报告,您需要使用 Cobertura.对于 TRX,您只会获得下载文件的链接.要创建 Cobertura 报告,您需要在您的测试项目 coverlet.collector
nuget 包中安装.这里有可以解决您的问题的代码:
To have there published report you need to use Cobertura. For TRX you will get only link to download file. And to create Cobertura report you need to install in your test projects coverlet.collector
nuget package. Here you have code which should fix your problem:
# You just added coverlet.collector to use 'XPlat Code Coverage'
- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
workingDirectory: $(Build.SourcesDirectory)
- task: DotNetCoreCLI@2
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
- script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
displayName: Create reports
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml
[2021 更新]
安装/运行自定义 ReportGenerator 工具不需要额外的任务:它现在是读取 coverage.cobertura.xml
文件的默认工具,并包含在 dotnet
CLI.
You don't need extra tasks to install/run the custom ReportGenerator tool: it is now the default tool for reading coverage.cobertura.xml
files and is included in the dotnet
CLI.
不过,默认情况下,它会将 cobertura xml 文件保存到代理的临时目录中.因此,您只需要更新 PublishCodeCoverageResults
任务的 summaryFileLocation
以指向临时目录并跳过中间人"任务.步骤:
By default, it will save the cobertura xml file to the temp directory on the agent, though. So, you just need to update the summaryFileLocation
of the PublishCodeCoverageResults
task to point to the temp directory and skip the "middle man" steps:
# You just added coverlet.collector to use 'XPlat Code Coverage'
- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
如果您有多个生成多个覆盖文件的测试项目,请在测试命令后使用这些步骤.它会在发布之前合并文件:
If you have multiple test projects which generates multiple coverage files please use these steps after test commad. It will merge files before publishing them:
- task: reportgenerator@4
displayName: "Merge code coverage reports"
inputs:
reports: "**/coverage.cobertura.xml"
targetdir: "$(Build.ArtifactStagingDirectory)/coverlet"
reporttypes: "Cobertura"
verbosity: "Verbose"
- task: PublishCodeCoverageResults@1
displayName: "Publish code coverage results"
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: "$(Build.ArtifactStagingDirectory)/coverlet/Cobertura.xml"
这篇关于发布管道 Azure Devops 代码覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!