Azure DevOps Code Coverage for .NET Core 3.1
Finally got it working with the help of a Microsoft MVP. Sharing the code from the test segment that worked.
- task: DotNetCoreCLI@2
displayName: "dotnet global test tool install"
inputs:
command: 'custom'
custom: 'tool'
arguments: 'install --global dotnet-reportgenerator-globaltool'
- script: dotnet test WebApp.Web.Tests/WebApp.Web.Tests.csproj --logger "trx;LogFileName=testresults.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/
displayName: 'dotnet test'
- script: reportgenerator "-reports:$(Build.SourcesDirectory)/TestResults/Coverage/coverage.cobertura.xml" "-targetDir:$(Build.SourcesDirectory)/TestResults/Coverage/Reports" -tag:$(Build.BuildNumber) -reportTypes:htmlInline
workingDirectory: $(Build.SourcesDirectory)/WebApp.Web.Tests
displayName: 'dotnet reportgenerator'
- task: PublishTestResults@2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
failTaskOnFailedTests: true
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'cobertura'
summaryFileLocation: $(Build.SourcesDirectory)/TestResults/Coverage/**/coverage.cobertura.xml
reportDirectory: $(Build.SourcesDirectory)/TestResults/Coverage/Reports
failIfCoverageEmpty: false
Resources that helped can be found here
The accepted solution got me close, but I had issues with installing the tool globally. The PATH gets updated in a global install, but is never reloaded so the reportgenerator could not be found. Making the following changes took care of that:
task: DotNetCoreCLI@2
displayName: 'Install dotnet-reportgenerator-globaltool'
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
- task: DotNetCoreCLI@2
displayName: 'Test $(buildConfiguration)'
inputs:
command: test
projects: $(solution)
arguments: --configuration $(BuildConfiguration) --collect "XPlat Code Coverage"
- script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetDir:$(Build.SourcesDirectory)/TestResults/Coverage/Reports -reporttypes:Cobertura
displayName: 'Create Code Coverage Reports'
- task: PublishCodeCoverageResults@1
displayName: 'Publish Code Coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)/TestResults/Coverage/Reports/Cobertura.xml
failIfCoverageEmpty: false