Login
A chronicle of the thoughts, learning experiences, ideas and actions of a tech junkie, .NET, JS and Mobile dev, aspiring entrepreneur, devout Christian and travel enthusiast.
[HOW TO] Generate .NET Core code coverage with Coverlet
In the early days of .NET Core, there was a reliable, built-in testing system but no code coverage tool to gain insight into the scope of testing being done. While the full .NET framework was spoilt for choice when it came to the selection of code coverage tools, from OpenCover to dotCover, there was little to nothing available for the nascent .NET Core.
Doing some research in those early days, it didn't seem like any of the established coverage tool developers were in a rush to add .NET Core support and who could blame them? There was no evidence this was not going to end up going the way of Silverlight, so I understand why they hedged their bets.
tonerdo along with some of the awesome .NET community created an open-source code coverage tool called Coverlet
. This tool integrates itself into the msbuild system, instruments code and cam generate coverage in a number of supported formats.
To set your project up for coverage, in your test project folders i.e for each test project, run the following in your terminal:
dotnet add package coverlet.msbuild
dotnet add package Microsoft.NET.Test.Sdk
With that, you are set to run tests and generate coverage.
If you are looking to generate coverage data for a single test project or generate separate coverage files for multiple tests, then following simple call would suffice. It generates coverage in JSON format and outputs a coverage
file.
dotnet test /p:CollectCoverage=true
If you want to specify the output format, then an additional flag needs to be added. The formats supported currently are json
, lcov
, opencover
, cobertura
and teamcity
. For example, to generate coverage in the opencover
format, run the following:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
To generate coverage in multiple formats, separate the required formats by a comma (,). For example, to generate both lcov
and json
formats, we run the following:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov,json
Further flags and options and documentation can be found at the project wiki here.
There may be a situation where you'd want to generate a single coverage file from multiple test projects, for example, reporting the coverage of an entire project instead of the component bits, then it gets a bit verbose but coverlet
still covers (terrible pun, I know) us. For example, if I have three test projects, descriptively named Test1
, Test2
and Test3
and I want to generate a single coverage file in the opencover
format, then I have to run the following in sequence:
dotnet test Test1/Test1.csproj /p:CollectCoverage=true /p:CoverletOutput="./results/"
dotnet test Test2/Test2.csproj /p:CollectCoverage=true /p:CoverletOutput="./results/" /p:MergeWith="./results/coverage.json"
dotnet test Test3/Test3.csproj /p:CollectCoverage=true /p:CoverletOutput="./results/" /p:MergeWith="./results/coverage.json" /p:CoverletOutputFormat="opencover"
To explain what the above process does. First, we have to generate the coverage in the json
form an only convert to our desired format (in this case lcov
when we get to the last test project. Second, we need to have a specified folder where all the test reports get dumped so they can be combined, in this case out output folder is ./results
. And finally as earlier mentioned, we specify our desired output format on the last test run ad that does the combining.
Hope this helps. Cheers.
Comments