If you want to publish your code coverage results in VSTS from a .NET Core project its not very obvious. Code Coverage in .NET Core seems a little lacking so we’ll need help from a very simple, well built and maintained library called Coverlet
We’ll start with a super simple project right now, we have one functionality, a class named
Addition that adds the values passed in. We have a single unit test that actually tests for this.
The code can be found here: https://github.com/alanisoftware/.netcore-codecoverage-vsts
Here is the class:
public class Addition
public int Add(int firstValue, int secondValue)
return firstValue + secondValue;
The UnitTest (using MSTest)
public class AdditionTests
public void WhenAddingTwoValues_ShouldReturnValueTotal()
var addition = new Addition(); // ACT
var result = addition.Add(1, 2); // ASSERT
We need to add
coverlet as a dependency for our UnitTest project. This can be easily done via command line:
dotnet add package coverlet.msbuild
Now lets go to VSTS, create a new Build Pipeline for your project (if you don’t already have one):
Follow the wizard until you reach the template screen:
Update this using the following yml configuration shown here:
There are 3 things happening in this file,
dotnet buildthis simply builds your dotnet core application.
dotnet testthis should point to your UnitTest project that you want to collect coverage results for.
There are 2 arguments we will pass:
/p:CollectCoverage=true this will enable code coverage with Coverlet
/p:CoverletOutputFormat=cobertura this will set the format for code coverage result to Cobertura. This is currently the format that is supported by VSTS.
PublishCodeCoverageResults@1 this command is a built-in powershell command in the VSTS agent that will publish your coverage results. The inputs will define the format (in this case Cobertura) and the path to your Code Coverage summary file. This is normally outputted in the same directory where you ran the
dotnet test command.
For more details about the various configuration for this task, find it here: https://docs.microsoft.com/en-us/vsts/pipelines/tasks/test/publish-code-coverage-results?view=vsts
Now click “Save and Run” to run your CI with your code coverage result. The new VSTS yaml configuration will be saved and pushed to your repository and the build will run immediately.
Once the build is complete you should see your code coverage result:
Note this will not update your “Code Coverage” tab. You will need another tool that will generate an HTML report from coverage summary, then you point the publish task to the report.
Some more resources:
- Source code: https://github.com/alanisoftware/.netcore-codecoverage-vsts
- Build plan for the sample project: https://alanisoftware.visualstudio.com/Demos/Demos%20Team/_build/results?buildId=8
- VSTS Publish Coverage task documentation: https://docs.microsoft.com/en-us/vsts/pipelines/tasks/test/publish-code-coverage-results?view=vsts
- Use YAML CI (config as code): https://docs.microsoft.com/en-us/vsts/pipelines/build/yaml?view=vsts
- Coverlet: https://github.com/tonerdo/coverlet
Lastly if you want to make the most of Code Coverage in .NET Core follow this guide by Scott Hanselman