Setting up Coveralls with Coverlet for a .NET Core project
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
For our test project, we’ll just use the quick example project available from the official Microsoft docs: Unit testing C# with MSTest and .NET Core.
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
The first thing to do here is sign into Coveralls, you can use either a GitHub or Bitbucket account. Coveralls supports a bunch of different programming languages. The support for .NET is community maintained and the project can be found here, it supports uploading output formats generated by older tools like OpenCover, Monocov and Visual Studio Coverage.
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.