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.
https://www.nuget.org/packages/MSTest.TestFramework/
You’ll need at least version 1.4.0.
Here is the commit where I upped my version, so up yours!
https://github.com/FaronBracy/RogueSharp/commit/10775b73baaec0785155ad2be287a5c255d0212b
Azure DevOps Continuous Build
The Azure DevOps continuous build should then be using the .NET Core task.
I added three distinct steps.
- Restore NuGet Packages
- Build
- Run Tests
It’s this third step for running tests that will get us code coverage.
![alt text](/img/test_step_setup.png “Checkbox for Publish test results and code coverage)
- 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.
View Build Results In Azure DevOps
Code Coverage Badge
Shields.IO supports code coverage badges for multiple build services.
The formatted URL for Azure DevOps looks like this:
https://img.shields.io/azure-devops/coverage/{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.