(Translated by https://www.hiragana.jp/)
GitHub - jakejacobs2020/coverlet: Cross platform code coverage for .NET
Skip to content

jakejacobs2020/coverlet

 
 

Repository files navigation

Coverlet

Build Status License: MIT

Driver Current version Downloads
coverlet.collector NuGet NuGet
coverlet.msbuild NuGet NuGet
coverlet.console NuGet NuGet

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.

Coverlet documentation reflect the current repository state of the features, not the released ones.
Check the changelog to understand if the documented feature you want to use has been officially released.

Main contents

Quick Start

Coverlet can be used through three different drivers

  • VSTest engine integration
  • MSBuild task integration
  • As a .NET Global tool (supports standalone integration tests)

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)

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 from possible known issue)

Installation

dotnet tool install --global coverlet.console

Usage

Visual Studio on Windows provides a way to perform code coverage.

The unit test project requires the coverlet.msbuild NuGet package.

Code coverage results are written to an XML file so that they can be processed by another tool. Azure Pipelines supports Cobertura and JaCoCo coverage result formats.

To convert Cobertura coverage results to a format that's human-readable, they can use a tool called ReportGenerator.

ReportGenerator provides many formats, including HTML. The HTML formats create detailed reports for each class in a .NET project.

Specifically, there's an HTML format called HtmlInline_AzurePipelines, which provides a visual appearance that matches Azure Pipelines.

dotnet new tool-manifest
dotnet tool install dotnet-reportgenerator-globaltool
dotnet add *.Tests package coverlet.msbuild

dotnet test --no-build \
  --configuration Release \
  /p:CollectCoverage=true \
  /p:CoverletOutputFormat=cobertura \
  /p:CoverletOutput=./TestResults/Coverage/
  
dotnet tool run reportgenerator \
  -reports:./*.Tests/TestResults/Coverage/coverage.cobertura.xml \
  -targetdir:./CodeCoverage \
  -reporttypes:HtmlInline_AzurePipelines

About

Cross platform code coverage for .NET

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%