适用于 .NET 的跨平台代码覆盖率
| Driver | Current version | Downloads |
|---|---|---|
| coverlet.MTP | ||
| coverlet.collector | ||
| coverlet.msbuild | ||
| coverlet.console |
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.
[!NOTE] Coverlet only supports modern .NET SDK-style projects.
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.
Coverlet can be used through four different drivers
Coverlet supports only SDK-style projects https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2019
dotnet add package coverlet.MTP
[!NOTE] Add the
coverlet.MTPpackage only to test projects that use the Microsoft Testing Platform. This package is designed for projects usingMicrosoft.Testing.Platform(MTP) as the test runner, not the traditional VSTest runner.
Coverlet integrates with the Microsoft Testing Platform as an extension. To enable coverage collection, run your tests with the --coverlet flag:
dotnet run --project <your-test-project> -- --coverlet
Or when using dotnet test with MTP-enabled projects:
dotnet test --coverlet
After the above command is run, coverage report files will be generated in the test results directory. By default, reports are generated in json and cobertura formats.
| Option | Description |
|---|---|
--coverlet |
Enable code coverage data collection |
--coverlet-output-format |
Output format(s) for coverage report (json, lcov, opencover, cobertura) |
--coverlet-include |
Include assemblies matching filters (e.g., [Assembly]Type) |
--coverlet-exclude |
Exclude assemblies matching filters (e.g., [Assembly]Type) |
--coverlet-include-test-assembly |
Include test assembly in coverage |
Example with options:
dotnet run --project <your-test-project> --coverlet --coverlet-output-format cobertura --coverlet-exclude "[xunit.]"
dotnet tool install --global coverlet.console
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.
[!WARNING]
coverlet.collectorandcoverlet.msbuildcannot be used with the Microsoft Testing Platform (MTP). This is especially relevant when using the nativedotnet testintegration introduced with .NET 10.Both packages rely on the VSTest infrastructure, while the Microsoft Testing Platform uses a different test execution architecture, which makes these integrations incompatible. (Use Microsoft.Testing.Platform in the VSTest mode of dotnet test)
If your test project targets .NET 10.0, where the Microsoft Testing Platform is enabled by default, you need to disable
TestingPlatformDotnetTestSupport:<PropertyGroup> <TestingPlatformDotnetTestSupport>false</TestingPlatformDotnetTestSupport> </PropertyGroup>DataCollectors element
Microsoft.Testing.Platform is not using data collectors. Instead it has the concept of in-process and out-of-process extensions. Each extension is configured by its respective configuration file or through the command line.
Most importantly hang and crash extension, and code coverage extension.
Instead, use the coverlet.MTP extension designed for Microsoft Testing Platform: The
coverlet.MTPpackage provides the equivalent functionality ofcoverlet.collectorbut is implemented as a native extension for Microsoft Testing Platform.
dotnet add package coverlet.collector
[!NOTE] You MUST add package only to test projects and if you create xunit test projects (
dotnet new xunit) you will find the reference already present incsprojfile because Coverlet is the default coverage tool for every .NET Core and >= .NET 8 applications, you've only to update to last version if needed. Addcoverlet.collectorORcoverlet.msbuildpackage in a test project.
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.
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
dotnet add package coverlet.msbuild
N.B. Typically you MUST add package only to test projects. Do not add coverlet.msbuild and coverlet.collector package in a test project.
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.
Coverlet generates code coverage information by going through the following process:
Coverlet supports coverage for deterministic builds. The solution at the moment is not optimal and need a workaround. Take a look at documentation.
Coverlet exposes opt-in performance improvements that are not yet enabled by default.
Each feature is controlled by an environment variable.
Setting a variable to 1 (or any non-empty value other than 0 / false) enables the feature.
When the variable is absent or falsy, Coverlet behaves exactly like the current stable release.
[!WARNING] Experimental features may change or be removed without notice. Please report any issues you find.
COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHEScope: SkipInlineAssignedAutoProperty / auto-property backing-field detection
Default: disabled (original O(N) LINQ scan, identical to master)
When enabled, Coverlet caches the set of compiler-generated k__BackingField names per declaring type so the per-field scan is performed at most once per type rather than on every constructor instruction.
This can measurably reduce instrumentation time for assemblies with many classes that use inline-initialised auto-properties.
| Scenario | Without flag (default) | With flag enabled |
|---|---|---|
| Per-constructor instruction | O(N) LINQ scan over all fields of the declaring type | O(1) HashSet<string> lookup |
| First visit of a declaring type | O(N) scan | O(N) scan — result is cached |
暂无开放 Issues,或尚未同步最近议题。