【发布时间】:2020-04-13 12:55:36
【问题描述】:
我有一个用 dotnet core 2.1 编写的 C# 项目,我正在尝试设置 Azure 管道,以便在 macOS 代理上运行时获得代码覆盖(我可以更改为其他代理,但理想情况下是管道将与系统无关)。到目前为止,我一直试图让coverlet 和reportgenerator 一起工作,但我一直遇到问题,例如Could not find data collector 'XPlat Code Coverage'。
我想要做的是确定代码覆盖率(似乎是哪一个 Coverlet),并以某种方式在 Azure 管道中生成和显示代码覆盖率报告。
这是我到目前为止的管道:
pool:
vmImage: macOS-latest
variables:
solution: 'src/MySolution.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Debug'
steps:
- task: DotNetCoreInstaller@1
displayName: 'Use .NET Core sdk 2.2.103'
inputs:
version: 2.2.103
- task: DotNetCoreCLI@2
displayName: 'Restore NuGet packages for $(solution)'
inputs:
command: 'restore'
projects: '$(solution)'
- task: DotNetCoreCLI@2
displayName: 'Build $(solution)'
inputs:
command: 'build'
projects: '$(solution)'
arguments: '-c $(buildConfiguration)'
- task: DotNetCoreCLI@2
continueOnError: true
inputs:
command: custom
custom: tool
arguments: install -g coverlet.console
displayName: Install Coverlet tool. This task will continue on error.
- task: DotNetCoreCLI@2
displayName: 'Run tests for $(solution) collecting code coverage result'
inputs:
command: test
projects: 'src/MySolution.SomeProject.Tests/*.csproj'
arguments: -c $(buildConfiguration) --collect:"XPlat Code Coverage"
- script: coverlet src/MySolution.SomeProject.Tests/bin/$(buildConfiguration)/netcoreapp2.1/MySolution.SomeProject.Tests.dll --target "dotnet" --targetargs "test src/MySolution.SomeProject.Tests --no-build"
displayName: Run Coverlet to get code coverage.
- task: DotNetCoreCLI@2
continueOnError: true
inputs:
command: custom
custom: tool
arguments: install -g dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
# This outputs Analyzing 0 classes, and an index.htm file is created, but not sure how to access it
- script: reportgenerator -reports:$(Build.SourcesDirectory)/coverage.json -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:HtmlInline_AzurePipelines
displayName: 'Create reports.'
# Not sure what this should be
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml
【问题讨论】:
-
在 windows/ubuntu 代理中可以用吗?
-
不,我只使用了 macOS,因为这是我们作为自托管代理设置的。我现在已经开始工作了,所以将我发现的内容发布为答案,希望对其他人有所帮助。
标签: yaml code-coverage azure-pipelines reportgenerator coverlet