Cross platform code coverage arrives for .NET Core

If you have done any node.js development and have been required to hit code coverage marks prior to check in or CI/CD builds; you probably have used a tool like istanbul to check your percentages.

image from instabul.js.org

Getting code coverage reports on the command line for .NET Core has been a gap, and unless you are using Visual Studio Test or a 3rd party pay for play; getting this information was still only Windows only.

A few weeks ago I attended a hackathon at the Microsoft MVP Summit and saw a glimpse into coverlet from Toni Solarin (@tonerdo) and he has hit the nail on the head!

Using the quick test example on the docs site: Unit testing C# with MSTest and .NET Core. I created the project(s), added the coverlet NuGet package and ran dotnet test.

Create the solution and projects.

# create the solution file
dotnet new sln -n testing-coverlet

# create the class library project
dotnet new classlib -n PrimeService

# create the mstest project
dotnet new mstest -n PrimeService.Tests

# add the projects to the solution file
dotnet sln add **/**/*.csproj

# add the reference to the classib project
cd PrimeService.Tests  
dotnet add reference ../PrimeService/PrimeService.csproj  

Copy the code examples from the docs.

To add coverlet, it is just a NuGet package. So, just like any other we can add it to the PrimeServices.Tests project.

dotnet add package coverlet.msbuild  

And because it integrates directly with dotnet test there is no other work to do. Just run dotnet test and pass the parameter /p:CollectCoverage=true to enable the output.

And we get the output:

Build completed.

Test run for /PrimeService.Tests/bin/Debug/netcoreapp2.0/PrimeService.Tests.d  
ll(.NETCoreApp,Version=v2.0)  
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170628-02  
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

Total tests: 5. Passed: 5. Failed: 0. Skipped: 0.  
Test Run Successful.  
Test execution time: 0.8314 Seconds

Calculating coverage result...  
  Generating report '/PrimeService.Tests/coverage.json'

+--------------+----------+
| Module       | Coverage |
+--------------+----------+
| PrimeService | 66%      |
+--------------+----------+

This is just the start of what I am sure to be much more to come. It is another open source .NET Core project, so contribute, provide feedback and praise this work!

My full example available here on Github

Tweet Post Share Update Email RSS

Hi, I'm Shayne Boyer, I write this site, work on ASP.NET Core content and Open Source, speak at national and community events while helping teams architect web and cloud applications.

Tags:
dotnetcore testing opensource