Publishing .NET Core code coverage in Azure Devops with .Net 6 gives very insightful information and does not require a lot of effort.
- Add the extension for Report Generator from the market place.
- Add the coverlet.collector NuGet package to (each of) the test project(s) that you run dotnet test against.
<PackageReference Include=”coverlet.collector” Version=”3.1.0">
<IncludeAssets> runtime;build;native;contentfiles;analyzers;buildtransitive; </IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
3. Next, update the pipeline yaml to ensure your dotnet test command looks like below along with the report generator task to combine the generated coverage reports for all the test projects and then publish the report.
# This sets the variable to disable automatic generation of default coverage report and will allow us to use the new report that we are generating via Report Generator
- task: PowerShell@2
displayName: Set Runtime Variable To disable automatic generation of coverage report
inputs:
targetType: ‘inline’
script: echo ‘##vso[task.setvariable variable=disable.coverage.autogenerate]true’# This step basically just runs the tests and collects the coverage generates the coverage report in temp directory for example: /opt/tfsagent/_work/_temp/<runid>/coverage.cobertura.xml
- task: DotNetCoreCLI@2
displayName: Test All Test Projects Under Solution
inputs:
command: test
projects: ‘$(LocalSolutionDir)/$(solution)’
arguments: ‘ — configuration $(BuildConfiguration) — no-build — collect:”XPlat Code Coverage”’# This step combines the coverage reports into one and also creates a combined html report
- task: reportgenerator@5
displayName: ‘Generate Combined Code Coverage Report’
inputs:
reports: ‘$(Agent.TempDirectory)/**/*.cobertura.xml’
targetdir: ‘$(Build.SourcesDirectory)/CoverageResults’
reporttypes: ‘HtmlInline_AzurePipelines;Cobertura;’
title: ‘Combined Code Coverage’
condition: succeededOrFailed()# This step publishes the coverage report and sets the file that will be used for summary information and where to pick up the details for the code coverage.
- task: PublishCodeCoverageResults@1
displayName: ‘Publish Code Coverage From Combined Coverage Report’
inputs:
codeCoverageTool: ‘Cobertura’
summaryFileLocation: ‘$(Build.SourcesDirectory)/CoverageResults/Cobertura.xml’
reportDirectory: ‘$(Build.SourcesDirectory)/CoverageResults’
condition: succeededOrFailed()
References:
https://www.jamescroft.co.uk/combining-multiple-code-coverage-results-in-azure-devops/
https://josh-ops.com/posts/azure-devops-code-coverage/
https://lassiautio.com/2020/03/31/code-coverage-with-azure-devops/
https://marketplace.visualstudio.com/items?itemName=Palmmedia.reportgenerator
https://danielpalme.github.io/ReportGenerator/
https://docs.microsoft.com/en-us/azure/devops/pipelines/test/codecoverage-for-pullrequests?view=azure-devops
Comments
Post a Comment