I have a C# project written in dotnet core 2.1 which I'm trying to set up an Azure Pipeline for so that I can get code coverage when running on a macOS agent (I can change to other agents, but ideally the pipeline would be system agnostic). So far I have been trying to get coverlet and reportgenerator to work together, but I keep running into problems, such as Could not find data collector 'XPlat Code Coverage'
.
What I would like to happen is the code coverage is determined (which coverlet seems to be doing) and a code coverage report generated and displayed in Azure pipelines in some manner.
This is the pipeline I have so far:
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
So I got it working. I had some how missed that you can specify the format that coverlet
uses. By default it outputs coverage.json
, but by giving the format as cobertura
it outputs coverage.cobertura.xml
. So this yaml script works:
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