coverlet.msbuild 2.1.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package coverlet.msbuild --version 2.1.0
NuGet\Install-Package coverlet.msbuild -Version 2.1.0
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.msbuild" Version="2.1.0">
  <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.msbuild --version 2.1.0
#r "nuget: coverlet.msbuild, 2.1.0"
#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.msbuild as a Cake Addin
#addin nuget:?package=coverlet.msbuild&version=2.1.0

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

coverlet Build status codecov License: MIT

Coverlet is a cross platform code coverage library for .NET Core, with support for line, branch and method coverage.

Installation

Available on NuGet

Visual Studio:

PM> Install-Package coverlet.msbuild

.NET Core CLI:

dotnet add package coverlet.msbuild

How It Works

Coverlet integrates with the MSBuild system and that allows it to go through the following process:

Before Tests Run

  • Locates the unit test assembly and selects all the referenced assemblies that have PDBs.
  • Instruments the selected assemblies by inserting code to record sequence point hits to a temporary file.

After Tests Run

  • Restore the original non-instrumented assembly files.
  • Read the recorded hits information from the temporary file.
  • Generate the coverage result from the hits information and write it to a file.

Note: The assembly you'd like to get coverage for must be different from the assembly that contains the tests

Usage

Coverlet doesn't require any additional setup other than including the NuGet package in the unit test project. It integrates with the dotnet test infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run.

Code Coverage

Enabling code coverage is as simple as setting the CollectCoverage property to true

dotnet test /p:CollectCoverage=true

After the above command is run, a coverage.json file containing the results will be generated in the root directory of the test project. A summary of the results will also be displayed in the terminal.

Coverage Output

Coverlet can generate coverage results in multiple formats, which is specified using the CoverletOutputFormat property. For example, the following command emits coverage results in the opencover format:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

Supported Formats:

  • json (default)
  • lcov
  • opencover
  • cobertura

The output of the coverage result can also be specified using the CoverletOutput property.

Threshold

Coverlet allows you to specify a coverage threshold below which it fails the build. This allows you to enforce a minimum coverage percent on all changes to your project.

dotnet test /p:CollectCoverage=true /p:Threshold=80

The above command will automatically fail the build if the line, branch or method coverage of any of the instrumented modules falls below 80%. You can specify what type of coverage to apply the threshold value to using the ThresholdType property. For example to apply the threshold check to only line coverage:

dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line

You can specify multiple values for ThresholdType by separating them with commas. Valid values include line, branch and method.

Excluding From Coverage

Attributes

You can ignore a method or an entire class from code coverage by creating and applying the ExcludeFromCodeCoverage attribute present in the System.Diagnostics.CodeAnalysis namespace.

Source Files

You can also ignore specific source files from code coverage using the ExcludeByFile property

  • Use single or multiple paths (separate by comma)
  • Use absolute or relative paths (relative to the project directory)
  • Use file path or directory path with globbing (e.g dir1/*.cs)
dotnet test /p:CollectCoverage=true /p:ExcludeByFile=\"../dir1/class1.cs,../dir2/*.cs,../dir3/**/*.cs,\"
Filters

Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions".

Syntax: /p:Exclude=[Assembly-Filter]Type-Filter

Wildcards

  • * ⇒ matches zero or more characters
  • ? ⇒ the prefixed character is optional

Examples

  • /p:Exclude="[*]*" ⇒ Excludes all types in all assemblies (nothing is instrumented)
  • /p:Exclude="[coverlet.*]Coverlet.Core.Coverage" ⇒ Excludes the Coverage class in the Coverlet.Core namespace belonging to any assembly that matches coverlet.* (e.g coverlet.core)
  • /p:Exclude="[*]Coverlet.Core.Instrumentation.*" ⇒ Excludes all types belonging to Coverlet.Core.Instrumentation namespace in any assembly
  • /p:Exclude="[coverlet.*.tests?]*" ⇒ Excludes all types in any assembly starting with coverlet. and ending with .test or .tests (the ? makes the s optional)
  • /p:Exclude=\"[coverlet.*]*,[*]Coverlet.Core*\" ⇒ Excludes assemblies matching coverlet.* and excludes all types belonging to the Coverlet.Core namespace in any assembly
dotnet test /p:CollectCoverage=true /p:Exclude="[coverlet.*]Coverlet.Core.Coverage"

You can specify multiple filter expressions by separting them with a comma (,). If you specify multiple filters, then you'll have to escape the surrounding quotes like this: /p:Exclude=\"[coverlet.*]*,[*]Coverlet.Core*\".

Cake Addin

If you're using Cake Build for your build script you can use the Cake.Coverlet addin to provide you extensions to dotnet test for passing coverlet arguments in a strongly typed manner.

Roadmap

  • Merging outputs (multiple test projects, one coverage result)
  • Support for more output formats (e.g. JaCoCo)
  • Console runner (removes the need for requiring a NuGet package)

Issues & Contributions

If you find a bug or have a feature request, please report them at this repository's issues section. Contributions are highly welcome, however, except for very small changes, kindly file an issue and let's have a discussion before you open a pull request.

Building The Project

Clone this repo:

git clone https://github.com/tonerdo/coverlet

Change directory to repo root:

cd coverlet

Execute build script:

dotnet msbuild build.proj

This will result in the following:

  • Restore all NuGet packages required for building
  • Build and publish all projects. Final binaries are placed into <repo_root>\build\<Configuration>
  • Build and run tests

These steps must be followed before you attempt to open the solution in an IDE (e.g. Visual Studio, Rider) for all projects to be loaded successfully.

Code of Conduct

This project enforces a code of conduct in line with the contributor covenant. See CODE OF CONDUCT for details.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

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 (12)

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

Package Downloads
Rocket.Surgery.Extensions.Testing.Coverlet

Package Description

Corvus.Testing.SpecFlow.NUnit

A metapackage that encapsulates the required dependencies when using Corvus.Testing.SpecFlow and Endjin's standard practises. Also simplifies the dependency management process when using tools like Dependabot.

Dolittle.Common.Specs

Package Description

Corvus.Testing.AzureFunctions.SpecFlow.NUnit

A metapackage that encapsulates the required dependencies when using Corvus.Testing.AzureFunctions.SpecFlow and Endjin's standard practises. Also simplifies the dependency management process when using tools like Dependabot.

Klinked.Cqrs

Simple to use CQRS library.

GitHub repositories (261)

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

Repository Stars
App-vNext/Polly
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
Jackett/Jackett
API Support for your favorite torrent trackers
dotnet/machinelearning
ML.NET is an open source and cross-platform machine learning framework for .NET.
reactiveui/refit
The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.
reactiveui/ReactiveUI
An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
Version Downloads Last updated
6.0.2 225,595 3/13/2024
6.0.1 489,476 2/20/2024
6.0.0 10,039,785 5/21/2023
3.2.0 13,913,556 10/29/2022
3.1.2 20,235,985 2/6/2022
3.1.1 538,384 1/30/2022
3.1.0 13,530,065 7/19/2021
3.0.3 11,074,570 2/21/2021
3.0.2 2,448,377 1/24/2021
3.0.1 617,326 1/16/2021
3.0.0 427,891 1/9/2021
2.9.0 18,787,011 5/30/2020
2.8.1 5,533,085 4/2/2020
2.8.0 7,445,079 1/3/2020
2.7.0 5,080,084 9/22/2019
2.6.3 2,645,872 7/1/2019
2.6.2 707,240 6/6/2019
2.6.1 1,203,864 5/8/2019
2.6.0 1,127,825 3/4/2019
2.5.1 2,607,026 1/17/2019
2.5.0 652,121 12/20/2018
2.4.0 1,000,916 11/28/2018
2.3.2 274,828 11/19/2018
2.3.1 715,722 10/16/2018
2.3.0 819,408 9/7/2018
2.2.1 480,358 8/11/2018
2.1.1 341,772 7/16/2018
2.1.0 157,072 7/6/2018
2.0.1 410,166 6/12/2018
2.0.0 106,682 5/17/2018
1.2.0 111,942 5/2/2018
1.1.1 56,274 4/17/2018
1.1.0 5,195 4/16/2018
1.0.2 47,988 4/2/2018
1.0.1 14,859 3/26/2018
1.0.0 107,378 3/21/2018