coverlet.collector 6.0.2

Prefix Reserved
dotnet add package coverlet.collector --version 6.0.2                
NuGet\Install-Package coverlet.collector -Version 6.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="coverlet.collector" Version="6.0.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add coverlet.collector --version 6.0.2                
#r "nuget: coverlet.collector, 6.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install coverlet.collector as a Cake Addin
#addin nuget:?package=coverlet.collector&version=6.0.2

// Install coverlet.collector as a Cake Tool
#tool nuget:?package=coverlet.collector&version=6.0.2                

Coverlet integration with VSTest (a.k.a. Visual Studio Test Platform)

Supported runtime versions:

Since version 6.0.0

  • .NET Core >= 6.0
  • .NET Framework >= 4.6.2

As explained in quick start section, to use collectors you need to run SDK v6.0.100 (LTS) or newer and your project file must reference coverlet.collector and a minimum version of Microsoft.NET.Test.Sdk.

A sample project file looks like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net6.0;net48</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  ...
  </ItemGroup>
...
</Project>

The reference to coverlet.collector package is included by default with xunit template test (dotnet new xunit), you only need to update the package for new versions like any other package reference.

With correct reference in place you can run coverage through default dotnet test CLI verbs:

dotnet test --collect:"XPlat Code Coverage"

or

dotnet publish
...
  ... -> C:\project\bin\Debug\netcoreapp3.0\testdll.dll
  ... -> C:\project\bin\Debug\netcoreapp3.0\publish\
...
dotnet vstest C:\project\bin\Debug\netcoreapp3.0\publish\testdll.dll --collect:"XPlat Code Coverage"

As you can see in case of vstest verb you must publish project before.

At the end of tests you'll find the coverage file data under default VSTest platform directory TestResults

Attachments:
  C:\git\coverlet\Documentation\Examples\VSTest\HelloWorld\XUnitTestProject1\TestResults\bc5e983b-d7a8-4f17-8c0a-8a8831a4a891\coverage.cobertura.xml
Test Run Successful.
Total tests: 1
     Passed: 1
 Total time: 2,5451 Seconds

You can change the output directory using the standard dotnet test switch --results-directory

NB: By design VSTest platform will create your file under a random named folder(guid string) so if you need stable path to load file to some gui report system(i.e. coveralls, codecov, reportgenerator etc..) that doesn't support glob patterns or hierarchical search, you'll need to manually move resulting file to a predictable folder

Coverlet options supported by VSTest integration

⚠️At the moment VSTest integration doesn't support all features of msbuild and .NET tool, for instance show result on console, report merging and threshold validation. We're working to fill the gaps.

PS: if you don't have any other way to merge reports(for instance your report generator doesn't support multi coverage file) you can for the moment exploit a trick reported by one of our contributor Daniel Paz(@p4p3) https://github.com/tonerdo/coverlet/pull/225#issuecomment-573896446

Default option (if you don't specify a runsettings file)

Without specifying a runsettings file and calling coverlet by just the name of the collector, the result of the generated coverage output is by default in cobertura format.

dotnet test --collect:"XPlat Code Coverage"

The output format of the coverage report can also be changed without a runsettings file by specifying it in a parameter. The supported formats are lcov, opencover, cobertura, teamcity, json (default coverlet proprietary format).

dotnet test --collect:"XPlat Code Coverage;Format=json"

It is even possible to specify the coverage output in multiple formats.

dotnet test --collect:"XPlat Code Coverage;Format=json,lcov,cobertura"

Advanced Options (Supported via runsettings)

These are a list of options that are supported by coverlet. These can be specified as datacollector configurations in the runsettings.

Option Summary
Format Coverage output format. These are either cobertura, json, lcov, opencover or teamcity as well as combinations of these formats.
Exclude Exclude from code coverage analysing using filter expressions.
ExcludeByAttribute Exclude a method, an entire class or assembly from code coverage decorated by an attribute.
ExcludeByFile Ignore specific source files from code coverage.
Include Explicitly set what to include in code coverage analysis using filter expressions.
IncludeDirectory Explicitly set which directories to include in code coverage analysis.
SingleHit Specifies whether to limit code coverage hit reporting to a single hit for each location.
UseSourceLink Specifies whether to use SourceLink URIs in place of file system paths.
IncludeTestAssembly Include coverage of the test assembly.
SkipAutoProps Neither track nor record auto-implemented properties.
DoesNotReturnAttribute Methods marked with these attributes are known not to return, statements following them will be excluded from coverage
DeterministicReport Generates deterministic report in context of deterministic build. Take a look at documentation for further informations.
ExcludeAssembliesWithoutSources Specifies whether to exclude assemblies without source. Options are either MissingAll, MissingAny or None. Default is MissingAll.

How to specify these options via runsettings?

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>json,cobertura,lcov,teamcity,opencover</Format>
          <Exclude>[coverlet.*.tests?]*,[*]Coverlet.Core*</Exclude> 
          <Include>[coverlet.*]*,[*]Coverlet.Core*</Include> 
          <ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute</ExcludeByAttribute>
          <ExcludeByFile>**/dir1/class1.cs,**/dir2/*.cs,**/dir3/**/*.cs,</ExcludeByFile> 
          <IncludeDirectory>../dir1/,../dir2/,</IncludeDirectory>
          <SingleHit>false</SingleHit>
          <UseSourceLink>true</UseSourceLink>
          <IncludeTestAssembly>true</IncludeTestAssembly>
          <SkipAutoProps>true</SkipAutoProps>
          <DeterministicReport>false</DeterministicReport>
          <ExcludeAssembliesWithoutSources>MissingAll,MissingAny,None</ExcludeAssembliesWithoutSources>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Filtering details are present on msbuild guide.

This runsettings file can easily be provided using command line option as given :

  • dotnet test --collect:"XPlat Code Coverage" --settings coverlet.runsettings
  • dotnet vstest C:\project\bin\Debug\netcoreapp3.0\publish\testdll.dll --collect:"XPlat Code Coverage" --settings coverlet.runsettings

Take a look at our HelloWorld sample.

Passing runsettings arguments through commandline

You can avoid passing a runsettings file to dotnet test driver by using the xml flat syntax in the command line.

For instance if you want to set the Format element as a runsettings option you can use this syntax:

dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=json,cobertura,lcov,teamcity,opencover

Take a look here for further information:https://github.com/microsoft/vstest/blob/main/docs/RunSettingsArguments.md

How it works

Coverlet integration is implemented with the help of datacollectors. When we specify --collect:"XPlat Code Coverage" VSTest platform tries to load coverlet collectors inside coverlet.collector.dll

  1. Out-of-proc Datacollector: The outproc collector run in a separate process(datacollector.exe/datacollector.dll) than the process in which tests are being executed(testhost*.exe/testhost.dll). This datacollector is responsible for calling into Coverlet APIs for instrumenting dlls, collecting coverage results and sending the coverage output file back to test platform.

  2. In-proc Datacollector: The in-proc collector is loaded in the testhost process executing the tests. This collector will be needed to remove the dependency on the process exit handler to flush the hit files and avoid to hit this serious known issue

Known Issues

For a comprehensive list of known issues check the detailed documentation KnownIssues.md

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (39)

Showing the top 5 NuGet packages that depend on coverlet.collector:

Package Downloads
Rocket.Surgery.Extensions.Testing.Coverlet

Package Description

Kasp.Test

some tools for fast developing back-end.

Uno.Extensions.Reactive.Testing

Extensions to accelerate your application development with Uno Platform, UWP and WinUI

EBK.Core.BuildingBlocks

Package Description

Coffee.NetCore

Package Description

GitHub repositories (1.6K)

Showing the top 5 popular GitHub repositories that depend on coverlet.collector:

Repository Stars
shadowsocks/shadowsocks-windows
A C# port of shadowsocks
jellyfin/jellyfin
The Free Software Media System
DevToys-app/DevToys
A Swiss Army knife for developers.
NickeManarin/ScreenToGif
🎬 ScreenToGif allows you to record a selected area of your screen, edit and save it as a gif or video.
dotnet/maui
.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
Version Downloads Last updated
6.0.2 29,488,251 3/13/2024
6.0.1 4,519,326 2/20/2024
6.0.0 54,685,329 5/21/2023
3.2.0 60,798,421 10/29/2022
3.1.2 98,295,986 2/6/2022
3.1.1 1,326,836 1/30/2022
3.1.0 52,459,200 7/19/2021
3.0.3 16,730,434 2/21/2021
3.0.2 24,946,509 1/24/2021
3.0.1 1,038,635 1/16/2021
3.0.0 692,521 1/9/2021
1.3.0 43,603,840 5/30/2020
1.2.1 6,150,123 4/2/2020
1.2.0 42,848,281 1/3/2020
1.1.0 2,558,478 9/22/2019
1.0.1 20,721,726 7/1/2019
1.0.0 574,900 6/6/2019