Publishing Test Coverage with .NET Core and VSTS Build Pipelines

Tarik A
Tarik A
Aug 30, 2018 · 3 min read

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)

[TestClass]
public class AdditionTests
{
[TestMethod]
public void WhenAddingTwoValues_ShouldReturnValueTotal()
{
// ARRANGE
var addition = new Addition();
// ACT
var result = addition.Add(1, 2);
// ASSERT
Assert.AreEqual(result, 3);
}
}

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,

  1. dotnet build this simply builds your dotnet core application.
  2. dotnet test this 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.

3. 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:

That’s it!

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:

Lastly if you want to make the most of Code Coverage in .NET Core follow this guide by Scott Hanselman

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade