运行 macOS DevOps 管道中的 coverlet 和 reportgenerator
Run coverlet and reportgenerator in a macOS DevOps Pipeline
我有一个用 dotnet core 2.1 编写的 C# 项目,我正在尝试为其设置 Azure 管道,以便在 macOS 代理上 运行 时可以获得代码覆盖率(我可以更改为其他代理,但理想情况下,管道将与系统无关)。到目前为止,我一直在努力让 coverlet and 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
您应该将以下 NuGet PackageReference 添加到您的项目 .csproj 文件中
<PackageReference Include="coverlet.collector" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
您可以按照 this link 设置您的管道
所以我让它工作了。我有点想念您可以指定 coverlet
使用的格式。默认情况下,它输出 coverage.json
,但通过将格式设置为 cobertura
,它输出 coverage.cobertura.xml
。所以这个 yaml 脚本有效:
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
displayName: 'Run tests for $(solution) collecting code coverage result'
inputs:
command: test
projects: 'src/MySolution.SomeProject.Tests/*.csproj'
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.
- script: coverlet src/MySolution.SomeProject.Tests/bin/$(buildConfiguration)/netcoreapp2.1/MySolution.SomeProject.Tests.dll --target "dotnet" --targetargs "test src/MySolution.SomeProject.Tests --no-build" --format cobertura
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
- script: reportgenerator -reports:$(Build.SourcesDirectory)/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:HtmlInline_AzurePipelines
displayName: 'Create reports.'
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)/coverage.cobertura.xml
我有一个用 dotnet core 2.1 编写的 C# 项目,我正在尝试为其设置 Azure 管道,以便在 macOS 代理上 运行 时可以获得代码覆盖率(我可以更改为其他代理,但理想情况下,管道将与系统无关)。到目前为止,我一直在努力让 coverlet and 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
您应该将以下 NuGet PackageReference 添加到您的项目 .csproj 文件中
<PackageReference Include="coverlet.collector" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
您可以按照 this link 设置您的管道
所以我让它工作了。我有点想念您可以指定 coverlet
使用的格式。默认情况下,它输出 coverage.json
,但通过将格式设置为 cobertura
,它输出 coverage.cobertura.xml
。所以这个 yaml 脚本有效:
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
displayName: 'Run tests for $(solution) collecting code coverage result'
inputs:
command: test
projects: 'src/MySolution.SomeProject.Tests/*.csproj'
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.
- script: coverlet src/MySolution.SomeProject.Tests/bin/$(buildConfiguration)/netcoreapp2.1/MySolution.SomeProject.Tests.dll --target "dotnet" --targetargs "test src/MySolution.SomeProject.Tests --no-build" --format cobertura
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
- script: reportgenerator -reports:$(Build.SourcesDirectory)/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:HtmlInline_AzurePipelines
displayName: 'Create reports.'
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)/coverage.cobertura.xml