Collecting Code Coverage Metrics for .NET Core Libraries in Azure DevOps

Last time I talked about how I added status badges to the Readme file for RogueSharp. One of the badges was for showing the Code Coverage results from my continuous Azure DevOps build.

Before you can have a badge display those numbers, you need to have a build that can actually generate and collect them. While searching I found a lot of information about how to do it for full .NET Framework projects but information on .NET Core was scarce.

Since it wasn’t very straightforward for me I’ll share what I did here. Maybe there is a better way now?

MSTest Version

The first issue I struggled with was realizing that the version of MSTest that was I was using did not support the option to collect code coverage.

You’ll need at least version 1.4.0.

Here is the commit where I upped my version, so up yours!

Azure DevOps Menu

Azure DevOps Continuous Build

The Azure DevOps continuous build should then be using the .NET Core task.

.NET Core Build Task

I added three distinct steps.

  1. Restore NuGet Packages
  2. Build
  3. Run Tests

Build Steps

It’s this third step for running tests that will get us code coverage.

Test Step Setup

  • Under Arguments check the box next to Publish test results and code coverage
  • The Arguments should be --configuration $(BuildConfiguration) --collect "Code coverage"

The --collect "Code coverage" bit will only work if you are on MSTest version 1.4.0 or higher. It also appears to be case sensitive so make sure you type it exactly as shown.

Viewing Code Coverage Metrics

Once this is setup trigger the continuous build. If you look in the summary you should see the code coverage showing up now.

Build Summary

View Build Results In Azure DevOps

Code Coverage Badge

Code coverage

Shields.IO supports code coverage badges for multiple build services.

The formatted URL for Azure DevOps looks like this:{ORGANIZATION}/{PROJECT}/{DEFINITION_ID}.svg

See my previous post on status badges/shields for more information

About Faron

Faron Bracy is a husband, father, and TechSmith employee. He maintains the open source library RogueSharp as a hobby project. Other hobbies include painting fantasy miniatures, playing board games, and playing retro video games with his family.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.