DEV Community

Cover image for How to include code coverage in Azure DevOps pipeline?
Nicola Biancolini
Nicola Biancolini

Posted on • Updated on • Originally published at binick.github.io

How to include code coverage in Azure DevOps pipeline?

By code coverage we mean the action of trying to measure how much of our code has been executed by our tests.
This sound like

TL;DR

Untested code is a broken code.
Definitely a strong statement but true in a way, we don't always manage to get enough coverage.
Often this happens because we don't have time, other times because despite having written tests we are not able to read the metrics.

So, how we can "humanize" code coverage metrics? And how we can generate its?

To answer at these questions I usually use two libraries.

GitHub logo coverlet-coverage / coverlet

Cross platform code coverage for .NET

to gather metrics, and

GitHub logo danielpalme / ReportGenerator

ReportGenerator converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.

for generate human-readable reports.

How can set-up coverlet?

I usually include coverlet.msbuild by MSBuild .targets Files - Visual Studio | Microsoft Docs.

For alternative ways to include coverlet into yout test project see also coverlet-coverage/coverlet: Cross platform code coverage for .NET (github.com).

How can set-up ReportGenerator?

In keeping with above to include ReportGenerator by MSBuild .targets Files - Visual Studio | Microsoft Docs.

Also this tool offer a various way to use it, you can find all ways onto official documentation ReportGenerator - converts coverage reports generated by coverlet.

How to wire-up all that?

To make everything work we need to add another MSBuild file.

And include this into your test project, something like this

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <Import Project="Tests.targets" />

</Project>
Enter fullscreen mode Exit fullscreen mode

Now everything you are able to run dotnet test you will able to inspect and analyze something like this

coverage-report)

coverage-report-detail

I think that is an amazing tool to understand at a glance which codes are covered and which not.

And now, how I can put it into Azure DevOps pipeline?

It would be nice if this report came was published into the Build pipeline report, don't you think? Maybe even include branch policies for it.

Well that's possible by use Publish Code Coverage Results task, something like this:

- task: PublishCodeCoverageResults@1
  displayName: Publish Code Coverage Results
  inputs:
    codeCoverageTool: 'cobertura'
    summaryFileLocation: '$(Build.SourcesDirectory)/artifacts/TestResults/$(_BuildConfig)/Reports/Summary/Cobertura.xml'
    continueOnError: true
    condition: always()
Enter fullscreen mode Exit fullscreen mode

We notice the summaryFileLocation argument, this means that we will push only one file to Azure DevOps why?

One unwrite note of Publish Code Coverage Results task or limitation, I don't know, is that the sum of covered lines, when we publish more reports, is take from the first file

This results in an unreliable result.

To fix that problem we can marge multiple reports into a summary reports so that can be publish it only one. One way to make it is the follow

and run MSBuild project into the pipeline with

- script: dotnet msbuild SummaryReportGenerator.proj /p:Configuration=$(Configuration)
  name: GenerateCodeCoverageSummary
  displayName: Generate code coverage summary
Enter fullscreen mode Exit fullscreen mode

Once you've done this the sum of covered lines on Build pipeline will true.

Oldest comments (0)