In this blog, i am going to explain following topics. This is my second part of Coverlet Integration with xUnit in c#.
- Merging Result
- Threshold
- Excluding from Coverage
With
Coverlet, you can combine the output of multiple coverage runs into a single
result.
Command
used:
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.
In result window we are getting the merge result of code coverage files.
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 code 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
(short name, i.e. the type name without the namespace, supported only):
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 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"
Include:
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 the Coverlet.Core namespace belonging to
any assembly that matches coverlet.* (e.g coverlet.core)
/p:Include="[coverlet.*.tests?]*"
=> Includes all types in any assembly starting with coverlet. and ending
with .test or .tests (the ? makes the s 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.
For Reference:
0 Comments