Projects Issues Blogs Broadcasts Resources
Project

coverlet

.NET Foundation Testing Code Analysis

Cross platform code coverage for .NET

Coverlet

Build Status License: MIT

Coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It works with .NET Framework on Windows and .NET Core on all supported platforms.

Main contents

Quick Start

Coverlet can be used through three different drivers

  • VSTest engine integration
  • MSBuild task integration
  • As a .NET Global tool

Coverlet supports only SDK-style projects https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2019

VSTest Integration (preferred due to known issue supports only .NET Core application)

At the moment collectors integration does not support .NET Framework application.

Installation

dotnet add package coverlet.collector

N.B. You MUST add package only to test projects and if you create xunit test projects (dotnet new xunit) you'll find the reference already present in csproj file because Coverlet is the default coverage tool for every .NET Core and >= .NET 5 applications, you've only to update to last version if needed.

Usage

Coverlet is integrated into the Visual Studio Test Platform as a data collector. To get coverage simply run the following command:

dotnet test --collect:"XPlat Code Coverage"

After the above command is run, a coverage.cobertura.xml file containing the results will be published to the TestResults directory as an attachment.

See documentation for advanced usage.

Requirements

  • You need to be running .NET Core SDK v2.2.401 or newer
  • You need to reference version 16.5.0 and above of Microsoft.NET.Test.Sdk
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />

MSBuild Integration (suffers of possible known issue)

Installation

dotnet add package coverlet.msbuild

N.B. You MUST add package only to test projects

Usage

Coverlet also integrates with the build system to run code coverage after tests. 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.

See documentation for advanced usage.

Requirements

Requires a runtime that support .NET Standard 2.0 and above

.NET Global Tool (guide, suffers of possible known issue)

Installation

dotnet tool install --global coverlet.console

Usage

The coverlet tool is invoked by specifying the path to the assembly that contains the unit tests. You also need to specify the test runner and the arguments to pass to the test runner using the --target and --targetargs options respectively. The invocation of the test runner with the supplied arguments must not involve a recompilation of the unit test assembly or no coverage result will be generated.

The following example shows how to use the familiar dotnet test toolchain:

coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"

Note: The --no-build flag is specified so that the /path/to/test-assembly.dll assembly isn't rebuilt

See documentation for advanced usage.

Requirements

.NET global tools rely on a .NET Core runtime installed on your machine https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#what-could-go-wrong

.NET Coverlet global tool requires .NET Core 2.2 and above

How It Works

Coverlet generates code coverage information by going 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.

Deterministic build support

Coverlet supports coverage for deterministic builds. The solution at the moment is not optimal and need a workaround.
Take a look at documentation.

Are you in trouble with some feature? Check on examples!

Known Issues

Unfortunately we have some known issues, check it here

Cake Add-In

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

Consume nightly build

We offer nightly build of master for all packages. See the documentation

Issues & Contributions

If you find a bug or have a feature request, please report them at this repository's issues section. See the CONTRIBUTING GUIDE for details on building and contributing to this project.

Coverlet Team

Author and owner

Co-maintainers

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.

For more information, see the .NET Foundation Code of Conduct.

License

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

Supported by the .NET Foundation

NuGet

coverlet.msbuild (fuget.org)
12,944,498 Total Downloads 16,120 Downloads Per Day Latest Version: 0 (May 30, 2020) Hide All Versions Show All Versions
Version Downloads Last Updated
5/30/2020 May 30, 2020
4/2/2020 April 2, 2020
1/3/2020 January 3, 2020
9/22/2019 September 22, 2019
7/1/2019 July 1, 2019
6/6/2019 June 6, 2019
5/8/2019 May 8, 2019
3/4/2019 March 4, 2019
1/17/2019 January 17, 2019
12/20/2018 December 20, 2018
11/28/2018 November 28, 2018
11/19/2018 November 19, 2018
10/16/2018 October 16, 2018
9/7/2018 September 7, 2018
8/11/2018 August 11, 2018
7/16/2018 July 16, 2018
7/6/2018 July 6, 2018
6/12/2018 June 12, 2018
5/17/2018 May 17, 2018
5/2/2018 May 2, 2018
4/17/2018 April 17, 2018
4/16/2018 April 16, 2018
4/2/2018 April 2, 2018
3/26/2018 March 26, 2018
3/21/2018 March 21, 2018
1697
222
76
Source Code

{{ 'https://github.com/tonerdo/coverlet' | no-protocol }}

NuGet