Setting up Coveralls with Coverlet for a .NET Core project

Image for post
Image for post
Source: https://kinsta.com/blog/wordpress-charts/

Code coverage is an important metric in measuring software quality, it indicates just how much of your code is covered by your test suite. Coverlet is a cross platform code coverage library for .NET Core and Coveralls is a hosted service for visualizing and analyzing historical coverage results. In this post, we’re gonna setup code coverage for a .NET Core app with Coverlet and publish those result to Coveralls.

Generating Coverage Results

The next step will be to add the Coverlet dependency to the unit test project

dotnet add package coverlet.msbuild

And because it integrates directly with MSBuild there is no other work to do. Simply run dotnet test /p:CollectCoverage=true and it’ll generate a coverage.json file after your tests are done running. To see how Coverlet works go here.

Uploading Results To Coveralls

At the time of writing, Coverlet is still quite new and its default json output isn’t yet supported but the good news is Coverlet also lets you use output results in the OpenCover format using the CoverletOutputFormat. So we can just update our test command to dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover and we’ll get a Coveralls compatible coverage.xml file generated for us.

Lets go ahead and add the coveralls.net package to the test suite project:

dotnet add package coveralls.net

Uploading results to Coveralls is now as simple as running:

<NUGET_PACKAGE_ROOT>\coveralls.net\0.7.0\tools\csmacnz.Coveralls.exe --opencover -i path/to/coverage.xml --useRelativePaths

In order for the command to work, the COVERALLS_REPO_TOKEN environment variable (which you can grab from the Coveralls website) has to be set.

And that’s it!

To see an example on how to automate this process with a CI server, take a look here.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store