coverlet.msbuild
6.0.2
Prefix Reserved
dotnet add package coverlet.msbuild --version 6.0.2
NuGet\Install-Package coverlet.msbuild -Version 6.0.2
<PackageReference Include="coverlet.msbuild" Version="6.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add coverlet.msbuild --version 6.0.2
#r "nuget: coverlet.msbuild, 6.0.2"
// Install coverlet.msbuild as a Cake Addin #addin nuget:?package=coverlet.msbuild&version=6.0.2 // Install coverlet.msbuild as a Cake Tool #tool nuget:?package=coverlet.msbuild&version=6.0.2
Coverlet integration with MSBuild
In this mode, 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.
If a property takes multiple comma-separated values please note that you will have to add escaped quotes around the string like this: /p:Exclude=\"[coverlet.*]*,[*]Coverlet.Core*\"
, /p:Include=\"[coverlet.*]*,[*]Coverlet.Core*\"
, or /p:CoverletOutputFormat=\"json,opencover\"
.
To achieve same behavior above using powershell you need to use the verbatim argument marker --%
like this:
dotnet test /p:CollectCoverage=true --% /p:CoverletOutputFormat=\"opencover,lcov\"
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
- teamcity
You can specify multiple output formats by separating them with a comma (,
).
The output of the coverage result can be specified using the CoverletOutput
property.
dotnet test /p:CollectCoverage=true /p:CoverletOutput='./result.json'
To specify a directory where all results will be written to (especially if using multiple formats), end the value with a /
.
dotnet test /p:CollectCoverage=true /p:CoverletOutput='./results/'
Note: escape characters might produce some unexpected results.
status | msbuild parameter |
---|---|
:heavy_check_mark: | /p:CoverletOutput="C:/GitHub/coverlet/artifacts/Reports/coverlet.core/" |
:heavy_check_mark: | /p:CoverletOutput="C:\\GitHub\\coverlet\\artifacts\\Reports\\coverlet.core\\" |
:heavy_check_mark: | /p:CoverletOutput="C:\GitHub\coverlet\artifacts\Reports\coverlet.core\\" |
:x: | /p:CoverletOutput="C:\GitHub\coverlet\artifacts\Reports\coverlet.core\" |
The Coverlet MSBuild task sets the CoverletReport
MSBuild item so that you can easily use the produced coverage reports. For example, using ReportGenerator to generate an html coverage report.
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../html-coverage-report" />
</Target>
TeamCity Output
Coverlet can output basic code coverage statistics using TeamCity service messages.
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=teamcity
The currently supported TeamCity statistics are:
TeamCity Statistic Key | Description |
---|---|
CodeCoverageL | Line-level code coverage |
CodeCoverageB | Branch-level code coverage |
CodeCoverageM | Method-level code coverage |
CodeCoverageAbsLTotal | The total number of lines |
CodeCoverageAbsLCovered | The number of covered lines |
CodeCoverageAbsBTotal | The total number of branches |
CodeCoverageAbsBCovered | The number of covered branches |
CodeCoverageAbsMTotal | The total number of methods |
CodeCoverageAbsMCovered | The number of covered methods |
Merging Results
With Coverlet, you can combine the output of multiple coverage runs into a single result.
dotnet test /p:CollectCoverage=true /p:MergeWith='/path/to/result.json'
The value given to /p:MergeWith
must be a path to Coverlet's own json result format. The results in result.json
will be read, and added to the new results written to by Coverlet. Check the sample.
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
.
You can do the same for Threshold
as well.
dotnet test /p:CollectCoverage=true /p:Threshold=\"80,100,70\" /p:ThresholdType=\"line,branch,method\"
By default, Coverlet will validate the threshold value against the coverage result of each module. The /p:ThresholdStat
option allows you to change this behaviour and can have any of the following values:
- Minimum (Default): Ensures the coverage result of each module isn't less than the threshold
- Total: Ensures the total combined coverage result of all modules isn't less than the threshold
- Average: Ensures the average coverage result of all modules isn't less than the threshold
The following command will compare the threshold value with the overall total coverage of all modules:
dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line /p:ThresholdStat=total
Excluding From Coverage
Attributes
You can ignore a method, an entire class or assembly from code coverage by creating and applying the ExcludeFromCodeCoverage
attribute present in the System.Diagnostics.CodeAnalysis
namespace.
You can also ignore additional attributes by using the ExcludeByAttribute
property
- Use single or multiple attributes (separate by comma)
- Use attribute name, attribute full name or fully qualified name of the attribute type (
Obsolete
,ObsoleteAttribute
,System.ObsoleteAttribute
)
dotnet test /p:CollectCoverage=true /p:ExcludeByAttribute="Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute"
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 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 theCoverlet.Core
namespace belonging to any assembly that matchescoverlet.*
(e.gcoverlet.core
)/p:Exclude="[*]Coverlet.Core.Instrumentation.*"
⇒ Excludes all types belonging toCoverlet.Core.Instrumentation
namespace in any assembly/p:Exclude="[coverlet.*.tests?]*"
⇒ Excludes all types in any assembly starting withcoverlet.
and ending with.test
or.tests
(the?
makes thes
optional)/p:Exclude=\"[coverlet.*]*,[*]Coverlet.Core*\"
⇒ Excludes assemblies matchingcoverlet.*
and excludes all types belonging to theCoverlet.Core
namespace in any assembly
dotnet test /p:CollectCoverage=true /p:Exclude="[coverlet.*]Coverlet.Core.Coverage"
Coverlet goes a step in the other direction by also letting you explicitly set what can be included using the Include
property.
Examples
/p:Include="[*]*"
⇒ Includes all types in all assemblies (everything is instrumented)/p:Include="[coverlet.*]Coverlet.Core.Coverage"
⇒ Includes the Coverage class in theCoverlet.Core
namespace belonging to any assembly that matchescoverlet.*
(e.gcoverlet.core
)/p:Include="[coverlet.*.tests?]*"
⇒ Includes all types in any assembly starting withcoverlet.
and ending with.test
or.tests
(the?
makes thes
optional)
Both Exclude
and Include
properties can be used together but Exclude
takes precedence. You can specify multiple filter expressions by separting them with a comma (,
).
You can also include coverage of the test assembly itself by setting /p:IncludeTestAssembly
to true
.
Skip auto-implemented properties
Neither track nor record auto-implemented properties.
Syntax: /p:SkipAutoProps=true
Instrument module wihtout local sources file
Syntax: /p:InstrumentModulesWithoutLocalSources=true
Methods that do not return
Methods that do not return can be marked with attributes to cause statements after them to be excluded from coverage.
Attributes can be specified with the following syntax.
Syntax: /p:DoesNotReturnAttribute="DoesNotReturnAttribute,OtherAttribute"
Note for Powershell / Azure DevOps users
To exclude or include multiple assemblies when using Powershell scripts or creating a .yaml file for an Azure DevOps build %2c
should be used as a separator. Msbuild will translate this symbol to ,
.
/p:Exclude="[*]*Examples?%2c[*]*Startup"
Azure DevOps builds do not require double quotes to be unescaped:
dotnet test --configuration $(buildConfiguration) --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/ /p:Exclude="[MyAppName.DebugHost]*%2c[MyAppNamet.WebHost]*%2c[MyAppName.App]*"
Note for Linux users
There is an issue with MSBuild on Linux that affects the ability to escape quotes while specifying multiple comma-separated values. Linux MSBuild automatically translates \
to /
in properties, tasks, etc. before using them, which means if you specified /p:CoverletOutputFormat=\"json,opencover\"
in an MSBuild script, it will be converted to /p:CoverletOutputFormat=/"json,opencover/"
before execution. This yields an error similar to the following:
MSBUILD : error MSB1006: Property is not valid. [/home/vsts/work/1/s/default.proj]
Switch: opencover/
You'll see this if directly consuming Linux MSBuild or if using the Azure DevOps MSBuild
task on a Linux agent.
The workaround is to use the .NET Core dotnet msbuild
command instead of using MSBuild directly. The issue is not present in dotnet msbuild
and the script will run with correctly escaped quotes.
SourceLink
Coverlet supports SourceLink custom debug information contained in PDBs. When you specify the /p:UseSourceLink=true
property, Coverlet will generate results that contain the URL to the source files in your source control instead of local file paths.
Deterministic build
Take a look at documentation for further information. To generate deterministic report the parameter is:
/p:DeterministicReport=true
Exclude assemblies without sources from coverage
The heuristic coverlet uses to determine if an assembly is a third-party dependency is based on the matching of the assembly`s source documents and the corresponding source files. This parameter has three different values to control the automatic assembly exclusion.
Parameter | Description |
---|---|
MissingAll | Includes the assembly if at least one document is matching. In case the ExcludeAssembliesWithoutSources parameter is not specified the default value is MissingAll . |
MissingAny | Includes the assembly only if all documents can be matched to corresponding source files. |
None | No assembly is excluded. |
Here is an example of how to specifiy the parameter:
/p:ExcludeAssembliesWithoutSources="MissingAny"
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages (11)
Showing the top 5 NuGet packages that depend on coverlet.msbuild:
Package | Downloads |
---|---|
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. |
|
Klinked.Cqrs.Logging
A logging decorator for the Klinked.Cqrs library that makes it easy to add logging to all commands, events, and queries. |
GitHub repositories (281)
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 | 6,348,476 | 3/13/2024 |
6.0.1 | 1,110,605 | 2/20/2024 |
6.0.0 | 13,571,271 | 5/21/2023 |
3.2.0 | 16,438,609 | 10/29/2022 |
3.1.2 | 22,544,790 | 2/6/2022 |
3.1.1 | 577,739 | 1/30/2022 |
3.1.0 | 14,373,869 | 7/19/2021 |
3.0.3 | 11,619,784 | 2/21/2021 |
3.0.2 | 2,542,580 | 1/24/2021 |
3.0.1 | 631,670 | 1/16/2021 |
3.0.0 | 449,527 | 1/9/2021 |
2.9.0 | 19,586,792 | 5/30/2020 |
2.8.1 | 5,758,485 | 4/2/2020 |
2.8.0 | 7,626,588 | 1/3/2020 |
2.7.0 | 5,241,001 | 9/22/2019 |
2.6.3 | 2,714,846 | 7/1/2019 |
2.6.2 | 717,283 | 6/6/2019 |
2.6.1 | 1,269,357 | 5/8/2019 |
2.6.0 | 1,140,146 | 3/4/2019 |
2.5.1 | 2,669,507 | 1/17/2019 |
2.5.0 | 674,402 | 12/20/2018 |
2.4.0 | 1,012,368 | 11/28/2018 |
2.3.2 | 279,594 | 11/19/2018 |
2.3.1 | 721,810 | 10/16/2018 |
2.3.0 | 823,194 | 9/7/2018 |
2.2.1 | 484,790 | 8/11/2018 |
2.1.1 | 343,276 | 7/16/2018 |
2.1.0 | 158,678 | 7/6/2018 |
2.0.1 | 420,293 | 6/12/2018 |
2.0.0 | 108,610 | 5/17/2018 |
1.2.0 | 114,667 | 5/2/2018 |
1.1.1 | 56,534 | 4/17/2018 |
1.1.0 | 5,449 | 4/16/2018 |
1.0.2 | 49,830 | 4/2/2018 |
1.0.1 | 15,955 | 3/26/2018 |
1.0.0 | 120,956 | 3/21/2018 |