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?
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 Continuous Build
The Azure DevOps continuous build should then be using the .NET Core task.
I added three distinct steps.
- Restore NuGet Packages
- Run Tests
It’s this third step for running tests that will get us code coverage.
- Under Arguments check the box next to
Publish test results and code coverage
- The Arguments should be
--configuration $(BuildConfiguration) --collect "Code coverage"
--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.
Code Coverage Badge
Shields.IO supports code coverage badges for multiple build services.
The formatted URL for Azure DevOps looks like this:
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.