### Install coverlet.console Global Tool
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Installs the coverlet console global tool. Ensure you have the .NET SDK installed.
```bash
dotnet tool install --global coverlet.console
```
--------------------------------
### Install coverlet.MTP Package
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Add the coverlet.MTP package to your test projects that use the Microsoft Testing Platform.
```bash
dotnet add package coverlet.MTP
```
--------------------------------
### Solution-Wide Coverlet Configuration with MSBuild
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Example of configuring coverlet for all test projects in a solution using Directory.Build.props. This ensures consistent settings across projects.
```xml
--coverlet --coverlet-file-prefix $(MSBuildProjectName)
```
--------------------------------
### Project Setup for testconfig.json
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
This XML snippet demonstrates how to configure your project file to ensure the testconfig.json file is copied to the output directory during the build process.
```xml
Always
```
--------------------------------
### Combined Debugging Example (Windows)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Enable multiple debugging features simultaneously on Windows by setting several environment variables. This is useful for comprehensive troubleshooting.
```shell
set COVERLET_MTP_DEBUG_WAIT=1 set COVERLET_ENABLETRACKERLOG=1 set COVERLET_MTP_EXCEPTIONLOG_ENABLED=1
```
--------------------------------
### Install coverlet.msbuild Package
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Add the coverlet.msbuild NuGet package to your test project. Ensure you only add this package to test projects and not both coverlet.msbuild and coverlet.collector.
```bash
dotnet add package coverlet.msbuild
```
--------------------------------
### Configure Coverlet Output Format and Exclusions
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Example demonstrating how to specify the output format for coverage reports and exclude specific assemblies using command-line options.
```bash
dotnet run --project --coverlet --coverlet-output-format cobertura --coverlet-exclude "[xunit.]"
```
--------------------------------
### Example Coverlet Appsettings JSON Configuration
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Demonstrates the structure and common keys for configuring Coverlet using the legacy appsettings.json format. Note that array values are comma-separated strings.
```json
{
"Coverlet": {
"Include": "[MyApp.*]*",
"Exclude": "[*.Tests]*",[*.Generated]*",
"ExcludeByAttribute": "GeneratedCode,ExcludeFromCodeCoverage",
"ExcludeByFile": "**/Migrations/*.cs",
"Format": "cobertura,json",
"UseSourceLink": false,
"SingleHit": false,
"IncludeTestAssembly": false,
"SkipAutoProps": true,
"DeterministicReport": false,
"ExcludeAssembliesWithoutSources": "MissingAll"
}
}
```
--------------------------------
### Example testconfig.json Configuration
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
This JSON snippet shows a recommended configuration for Coverlet within the testconfig.json file, specifying include/exclude filters, output formats, and other coverage options.
```json
{
"platformOptions": {
"Coverlet": {
"include": "[MyApp.*]*",
"exclude": "[*.Tests]*",
"excludeByAttribute": "GeneratedCode,ExcludeFromCodeCoverage,ExcludeFromCodeCoverageAttribute,CompilerGeneratedAttribute",
"excludeByFile": "**/Migrations/*.cs",
"format": "cobertura,json",
"useSourceLink": false,
"singleHit": false,
"includeTestAssembly": false,
"skipAutoProps": true,
"deterministicReport": false,
"excludeAssembliesWithoutSources": "MissingAll"
}
}
}
```
--------------------------------
### Test Host Waiting for Debugger
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Example output indicating that the test host is waiting for a debugger to attach.
```text
Starting test execution, please wait...
Logging Vstest Diagnostics in file: C:\git\coverletissue\collectorlog\XUnitTestProject1\log.txt
Host debugging is enabled. Please attach debugger to testhost process to continue.
Process Id: 800, Name: dotnet
```
--------------------------------
### Install coverlet.msbuild via Package Manager Console
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md
Use this command in the Package Manager console to install a specific nightly version of coverlet.msbuild. Ensure you replace the version placeholder with the actual nightly build version.
```powershell
PM> Install-Package coverlet.msbuild -Version X.X.X-preview.X.XXX -Source https://pkgs.dev.azure.com/tonerdo/coverlet/_packaging/coverlet-nightly/nuget/v3/index.json
```
```powershell
PM> Install-Package coverlet.msbuild -Version 6.0.4-preview.4.g5de0ad7d60 -Source https://pkgs.dev.azure.com/tonerdo/coverlet/_packaging/coverlet-nightly/nuget/v3/index.json
```
--------------------------------
### Install coverlet.msbuild via .NET CLI
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md
Use this command with the .NET CLI to add a specific nightly version of coverlet.msbuild to your project. Replace the version placeholder with the actual nightly build version.
```bash
dotnet add package coverlet.msbuild --version X.X.X-preview.X.XXX --source https://pkgs.dev.azure.com/tonerdo/coverlet/_packaging/coverlet-nightly/nuget/v3/index.json
```
```bash
dotnet add package coverlet.msbuild --version 6.0.4-preview.4.g5de0ad7d60 --source https://pkgs.dev.azure.com/tonerdo/coverlet/_packaging/coverlet-nightly/nuget/v3/index.json
```
--------------------------------
### MSBuild Parameter Escaping Example
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
Demonstrates correct escaping for MSBuild parameters that accept multiple comma-separated values, such as `Exclude`, `Include`, and `CoverletOutputFormat`.
```powershell
dotnet test /p:CollectCoverage=true --% /p:CoverletOutputFormat="opencover,lcov"
```
--------------------------------
### Run Coverlet with Dotnet Test
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
This example shows how to invoke Coverlet to generate code coverage for a .NET test assembly using the `dotnet test` command. Ensure the test assembly is not rebuilt by using the `--no-build` flag.
```bash
coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"
```
--------------------------------
### Configure CoverletOutput Path in MSBuild
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
Example of configuring the `CoverletOutput` path within an MSBuild target. Note the use of escaped backslashes for Windows paths.
```xml
```
--------------------------------
### Record filtered benchmark results
Source: https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.benchmark.tests/HowTo.md
This example shows how to record only specific benchmark results by using the `-BenchmarkFilter` parameter. The filter is case-insensitive and applies to the Method column.
```powershell
# Record only InstrumentationOptionsBenchmarks rows
pwsh scripts/Update-BenchmarkHistory.ps1 `
-ArtifactsRoot "artifacts/bin/coverlet.core.benchmark.tests/release_net10.0" `
-CoverletVersion "6.0.5" `
-BenchmarkFilter "InstrumentationOptions"
```
--------------------------------
### Generate Coverage in Multiple Formats
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
To generate coverage in multiple formats simultaneously, repeat the '--coverlet-output-format' option for each desired format. This example generates JSON, Cobertura, and LCOV formats.
```bash
dotnet run TestProject.dll --coverlet --coverlet-output-format json --coverlet-output-format cobertura --coverlet-output-format lcov
```
--------------------------------
### Build and Run Coverlet Benchmarks
Source: https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.benchmark.tests/HowTo.md
Build the project in release mode and then navigate to the output directory to run the benchmarks.
```bash
dotnet build test/coverlet.core.benchmark.tests -c release
cd artifacts/bin/coverlet.core.benchmark.tests/release_net10.0
./coverlet.core.benchmark.tests.exe
```
--------------------------------
### Show All Coverlet MTP Options
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Run this command to display all available command-line options for the coverlet.MTP extension.
```bash
dotnet exec --help
```
--------------------------------
### Display Coverlet Help Information
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Run this command to view all available options and arguments for the Coverlet global tool. This is useful for understanding its capabilities and syntax.
```bash
coverlet --help
```
--------------------------------
### Install coverlet.collector Package
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Add the coverlet.collector package to your test projects. This is necessary for enabling code coverage collection with VSTest.
```bash
dotnet add package coverlet.collector
```
--------------------------------
### Azure DevOps Build Exclusion Example
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
Azure DevOps builds do not require double quotes to be unescaped when specifying exclusions.
```shell
dotnet test --configuration $(buildConfiguration) --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/ /p:Exclude="[MyAppName.DebugHost]*%2c[MyAppName.WebHost]*%2c[MyAppName.App]*"
```
--------------------------------
### Set Coverage Threshold Type
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Specify the type of coverage (line, branch, or method) to which the threshold applies. This example applies the threshold only to line coverage.
```bash
coverlet --target --targetargs --threshold 80 --threshold-type line
```
--------------------------------
### List Available Benchmarks
Source: https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.benchmark.tests/HowTo.md
Use the --list flat argument to display a flat list of all benchmark full names.
```bash
./coverlet.core.benchmark.tests.exe --list flat
```
--------------------------------
### Generate NuGet Packages
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/VSTest/DeterministicBuild/HowTo.md
Run this command from the repository root to generate the necessary NuGet packages before proceeding with deterministic builds.
```powershell
dotnet pack
```
--------------------------------
### Coverlet.MTP Two-Process Architecture
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Illustrates the interaction between the Controller and Test Host processes in Coverlet.MTP.
```mermaid
flowchart LR
subgraph Controller["CONTROLLER PROCESS"]
direction TB
THC["builder.TestHostControllers"]
PLH["AddProcessLifetimeHandler"]
EVP["AddEnvironmentVariableProvider"]
THC --> PLH
THC --> EVP
end
subgraph TestHost["TEST HOST PROCESS"]
direction TB
TH["builder.TestHost"]
TSLH["AddTestSessionLifetimeHandle"]
DC["AddDataConsumer"]
TALC["AddTestApplicationLifecycleCallbacks"]
TH --> TSLH
TH --> DC
TH --> TALC
end
Controller -->|"Environment Variables
Process Lifecycle Events"| TestHost
style Controller fill:#e6f3ff,stroke:#0066cc,stroke-width:2px
style TestHost fill:#e6ffe6,stroke:#009900,stroke-width:2px
```
--------------------------------
### Exclude by Filter Expression
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Use filter expressions for fine-grained control over what code is excluded from coverage. This example excludes a specific class within assemblies matching a pattern.
```bash
coverlet --target --targetargs --exclude "[coverlet.*]Coverlet.Core.Coverage"
```
--------------------------------
### Clone and Build Coverlet
Source: https://github.com/coverlet-coverage/coverlet/blob/master/CONTRIBUTING.md
Clone the Coverlet repository and build the project using standard dotnet commands. Ensure you restore, build, and pack the project before running tests.
```bash
git clone https://github.com/coverlet-coverage/coverlet.git
cd coverlet
dotnet restore
dotnet build --no-restore
dotnet pack -c Debug
```
--------------------------------
### Record all benchmark results
Source: https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.benchmark.tests/HowTo.md
Use this command to record all benchmark results for a specific Coverlet version. Ensure the `ArtifactsRoot` points to the correct benchmark artifacts directory.
```powershell
# Record all benchmark results for a specific version
pwsh scripts/Update-BenchmarkHistory.ps1 `
-ArtifactsRoot "artifacts/bin/coverlet.core.benchmark.tests/release_net10.0" `
-CoverletVersion "6.0.5"
```
--------------------------------
### Include Specific Assemblies
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Use the '--coverlet-include' option with a filter to include only specific assemblies in the coverage report. The filter '[MyApp.]' includes all assemblies starting with 'MyApp.'.
```bash
dotnet exec TestProject.dll --coverlet --coverlet-include "[MyApp.]"
```
--------------------------------
### Configuration Precedence Order
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Illustrates the order in which Coverlet determines configuration values, from highest to lowest priority. Command line options always take precedence.
```text
Priority 1: Command line options (always take precedence)
Priority 2: testconfig.json (app-specific > generic)
Priority 3: coverlet.mtp.appsettings.json (legacy)
Priority 4: Built-in defaults (only when no configuration file exists)
```
--------------------------------
### Passing RunSettings Arguments via Command Line
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
Configure Coverlet coverage options directly on the command line using the '-- DataCollectionRunSettings...' syntax, avoiding the need for a separate .runsettings file.
```shell
dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=json,cobertura,lcov,teamcity,opencover
```
--------------------------------
### Enable Experimental Feature (Windows CMD)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Use this command to enable the COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE feature in Windows Command Prompt. Setting the environment variable to '1' activates the feature.
```cmd
# Windows (Command Prompt)
set COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE=1
```
--------------------------------
### Include by Filter Expression
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Use filter expressions to explicitly include specific code elements for coverage analysis. This example includes a specific class within assemblies matching a pattern.
```bash
--include "[coverlet.*]Coverlet.Core.Coverage"
```
--------------------------------
### Deterministic Cobertura Report Example
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/DeterministicBuild.md
This XML snippet shows a deterministic Cobertura coverage report generated by Coverlet. Note the empty `` element and the deterministic `filename` fragment `/_/...`.
```xml
...
```
--------------------------------
### Enable Debugger Launch (Windows)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Set this environment variable on Windows to launch a debugger when coverlet.MTP initializes. This allows you to attach a debugger early in the process.
```shell
set COVERLET_MTP_DEBUG=1
```
--------------------------------
### Enable Debugger Launch (Linux/macOS)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Set this environment variable on Linux or macOS to launch a debugger when coverlet.MTP initializes. This allows you to attach a debugger early in the process.
```shell
export COVERLET_MTP_DEBUG=1
```
--------------------------------
### Coverage Output Location
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
Coverage results, including the `coverage.cobertura.xml` file, are typically found in the `TestResults` directory within the VSTest platform's output. The specific folder is a randomly named GUID.
```text
Attachments:
C:\git\coverlet\Documentation\Examples\VSTest\HelloWorld\XUnitTestProject1\TestResults\bc5e983b-d7a8-4f17-8c0a-8a8831a4a891\coverage.cobertura.xml
Test Run Successful.
Total tests: 1
Passed: 1
Total time: 2.5451 Seconds
```
--------------------------------
### VSTest Process Termination Warning Example
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/KnownIssues.md
This output shows a common warning and zero coverage result when VSTest stops the process execution early. This can happen due to slow process shutdown or other factors delaying exit.
```bash
Calculating coverage result...
C:\Users\REDACTED\.nuget\packages\coverlet.msbuild\6.0.0\build\netstandard2.0\coverlet.msbuild.targets(21,5): warning : [coverlet] Hits file:'C:\Users\REDACTED\AppData\Local\Temp\testApp_ac32258b-fd4a-4bb4-824c-a79061e97c31' not found for module: 'testApp' [C:\Users\REDACTED\Documents\repo\testapp\testapp.Tests\testapp.Tests.csproj]
C:\Users\REDACTED\.nuget\packages\coverlet.msbuild\6.0.0\build\netstandard2.0\coverlet.msbuild.targets(21,5): warning : [coverlet] Hits file:'C:\Users\REDACTED\AppData\Local\Temp\testApp.Tests_ac32258b-fd4a-4bb4-824c-a79061e97c31' not found for module: 'testApp.Tests' [C:\Users\REDACTED\Documents\repo\testapp\testapp.Tests\testapp.Tests.csproj]
Generating report 'C:\Users\REDACTED\Documents\repo\testapp\lcov.info'
+---------------+------+--------+--------+
| Module | Line | Branch | Method |
+---------------+------+--------+--------+
| testApp | 0% | 0% | 0% |
+---------------+------+--------+--------+
| testApp.Tests | 0% | 100% | 0% |
+---------------+------+--------+--------+
+---------+------+--------+--------+
| | Line | Branch | Method |
+---------+------+--------+--------+
| Total | 0% | 0% | 0% |
+---------+------+--------+--------+
| Average | 0% | 0% | 0% |
+---------+------+--------+--------+
```
--------------------------------
### SourceLink Integration with Coverlet
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Use the `--use-source-link` flag to generate coverage results with source file URLs from source control instead of local paths.
```bash
coverlet --use-source-link
```
--------------------------------
### Copy Coverlet Configuration File
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Ensure the coverlet.mtp.appsettings.json file is copied to the output directory. This is crucial for the configuration to be loaded at runtime.
```xml
Always
```
--------------------------------
### Build Packages with Dotnet CLI
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ReleasePlan.md
Use this command to build release packages for coverlet. Ensure you are building from the master branch to avoid metadata link issues. The output shows successful builds for various coverlet components across different .NET runtimes.
```shell
dotnet pack -c release /p:TF_BUILD=true /p:PublicRelease=true
...
coverlet.core net8.0 succeeded (0,3s) → artifacts\bin\coverlet.core\release_net8.0\coverlet.core.dll
coverlet.core net9.0 succeeded (0,3s) → artifacts\bin\coverlet.core\release_net9.0\coverlet.core.dll
coverlet.core net10.0 succeeded (0,3s) → artifacts\bin\coverlet.core\release_net10.0\coverlet.core.dll
coverlet.msbuild.tasks net9.0 succeeded (0,3s) → artifacts\bin\coverlet.msbuild.tasks\release_net9.0\coverlet.msbuild.tasks.dll
coverlet.MTP net8.0 succeeded (0,3s) → artifacts\bin\coverlet.MTP\release_net8.0\coverlet.MTP.dll
coverlet.console net10.0 succeeded (0,3s) → artifacts\bin\coverlet.console\release_net10.0\coverlet.console.dll
coverlet.core netstandard2.0 succeeded (0,1s) → artifacts\bin\coverlet.core\release_netstandard2.0\coverlet.core.dll
coverlet.msbuild.tasks net10.0 succeeded (0,5s) → artifacts\bin\coverlet.msbuild.tasks\release_net10.0\coverlet.msbuild.tasks.dll
coverlet.collector net10.0 succeeded (0,5s) → artifacts\bin\coverlet.collector\release_net10.0\coverlet.collector.dll
coverlet.MTP net10.0 succeeded (0,5s) → artifacts\bin\coverlet.MTP\release_net10.0\coverlet.MTP.dll
coverlet.collector net9.0 succeeded (0,6s) → artifacts\bin\coverlet.collector\release_net9.0\coverlet.collector.dll
coverlet.console net9.0 succeeded (0,3s) → artifacts\bin\coverlet.console\release_net9.0\coverlet.console.dll
coverlet.msbuild.tasks net8.0 succeeded (0,5s) → artifacts\bin\coverlet.msbuild.tasks\release_net8.0\coverlet.msbuild.tasks.dll
coverlet.collector net8.0 succeeded (0,4s) → artifacts\bin\coverlet.collector\release_net8.0\coverlet.collector.dll
coverlet.console net8.0 succeeded (0,0s) → artifacts\publish\coverlet.console\release_net8.0\
coverlet.MTP netstandard2.0 succeeded (0,2s) → artifacts\bin\coverlet.MTP\release_netstandard2.0\coverlet.MTP.dll
coverlet.console net10.0 succeeded (0,0s) → artifacts\publish\coverlet.console\release_net10.0\
coverlet.msbuild.tasks netstandard2.0 succeeded (0,1s) → artifacts\bin\coverlet.msbuild.tasks\release_netstandard2.0\coverlet.msbuild.tasks.dll
coverlet.console net9.0 succeeded (0,1s) → artifacts\publish\coverlet.console\release_net9.0\
...
```
--------------------------------
### Configure Project for MTP and Coverlet
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Configure your .NET project file to enable the Microsoft Testing Platform runner and include necessary NuGet packages for MTP and coverlet integration.
```xml
net8.0
Exe
true
true
```
--------------------------------
### Integration Execution Pattern
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.Architecture.md
Illustrates the sequence of interactions between Coverlet packages and the test execution host during integration.
```mermaid
sequenceDiagram
participant I as Integration package
participant C as coverlet.core
participant T as Test execution host
I->>C: Prepare instrumentation plan
C->>C: Rewrite IL + create tracker metadata
I->>T: Start test execution
T-->>C: Persist hit data (during/after run)
I->>C: Load hits + calculate coverage
C-->>I: Coverage result(s)
I-->>I: Write reports / apply integration-specific behavior
```
--------------------------------
### Configure Test Project for Coverlet
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/MSBuild/DeterministicBuild/HowTo.md
Update your test project's .csproj file to include the coverlet.msbuild package and ensure necessary configurations for deterministic builds.
```xml
net8.0
false
all
runtime; build; native; contentfiles; analyzers
all
runtime; build; native; contentfiles; analyzers; buildtransitive
```
--------------------------------
### Enable Code Coverage Collection with MSBuild
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Run your tests using the `dotnet test` command with the `CollectCoverage` property set to `true`. This will generate a `coverage.json` file with the results and display a summary in the terminal.
```bash
dotnet test /p:CollectCoverage=true
```
--------------------------------
### Running Tests with Coverlet via vstest
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
Execute tests using the 'vstest' command-line tool and enable Coverlet code coverage collection with a specified runsettings file.
```shell
dotnet vstest C:\project\bin\Debug\netcoreapp3.0\publish\testdll.dll --collect:"XPlat Code Coverage" --settings coverlet.runsettings
```
--------------------------------
### Build Local Source Code with Dotnet CLI
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Use this command to build your local source code when testing updates or fixing issues. Ensure you are in the repository's root directory.
```text
D:\git\coverlet (fixjsonserializerbug -> origin)
λ dotnet build
Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 52.23 ms for D:\git\coverlet\test\coverlet.testsubject\coverlet.testsubject.csproj.
Restore completed in 58.97 ms for D:\git\coverlet\src\coverlet.console\coverlet.console.csproj.
Restore completed in 59 ms for D:\git\coverlet\src\coverlet.core\coverlet.core.csproj.
Restore completed in 59.17 ms for D:\git\coverlet\src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj.
Restore completed in 59.26 ms for D:\git\coverlet\src\coverlet.collector\coverlet.collector.csproj.
Restore completed in 60.1 ms for D:\git\coverlet\test\coverlet.collector.tests\coverlet.collector.tests.csproj.
Restore completed in 60.42 ms for D:\git\coverlet\test\coverlet.core.performancetest\coverlet.core.performancetest.csproj.
Restore completed in 60.47 ms for D:\git\coverlet\test\coverlet.core.tests\coverlet.core.tests.csproj.
Restore completed in 22.85 ms for D:\git\coverlet\test\coverlet.core.tests\coverlet.core.tests.csproj.
coverlet.testsubject -> D:\git\coverlet\test\coverlet.testsubject\bin\Debug\net8.0\coverlet.testsubject.dll
coverlet.core -> D:\git\coverlet\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll
coverlet.msbuild.tasks -> D:\git\coverlet\src\coverlet.msbuild.tasks\bin\Debug\netstandard2.0\coverlet.msbuild.tasks.dll
coverlet.collector -> D:\git\coverlet\src\coverlet.collector\bin\Debug\netstandard2.0\coverlet.collector.dll
coverlet.console -> D:\git\coverlet\src\coverlet.console\bin\Debug\net8.0\coverlet.console.dll
coverlet.core.performancetest -> D:\git\coverlet\test\coverlet.core.performancetest\bin\Debug\net8.0\coverlet.core.performancetest.dll
coverlet.core.tests -> D:\git\coverlet\test\coverlet.core.tests\bin\Debug\net8.0\coverlet.core.tests.dll
coverlet.collector.tests -> D:\git\coverlet\test\coverlet.collector.tests\bin\Debug\net8.0\coverlet.collector.tests.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:07.42
D:\git\coverlet (fixjsonserializerbug -> origin)
```
--------------------------------
### Run Coverlet with Custom Executable
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
Use this command to run Coverlet with a custom executable as the target. Coverlet will instrument assemblies in the specified directory before executing the target.
```bash
coverlet "/integrationtests" --target "/application/runner.exe"
```
--------------------------------
### Run Tests with Coverlet via dotnet test
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Run your tests using `dotnet test` with the `--coverlet` flag when your project is configured for Microsoft Testing Platform.
```bash
dotnet test --coverlet
```
--------------------------------
### Add coverlet.msbuild to MSBuild project file
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md
Include this PackageReference in your .csproj file to use a specific nightly version of coverlet.msbuild. Replace the version placeholder with the actual nightly build version.
```xml
```
```xml
```
--------------------------------
### Verify Deterministic Build Artifact
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/MSBuild/DeterministicBuild/HowTo.md
Check for the presence of the CoverletSourceRootsMapping file in the output directory to confirm that deterministic build settings were applied correctly.
```text
bin\Debug\net8.0\CoverletSourceRootsMapping_XUnitTestProject1
```
--------------------------------
### Configure NuGet.Config for Nightly Builds
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md
Add this configuration to your root NuGet.Config file to include the coverlet nightly build feed.
```xml
```
--------------------------------
### Run Code Coverage with Development Build
Source: https://github.com/coverlet-coverage/coverlet/blob/master/CONTRIBUTING.md
Execute code coverage using a development build of Coverlet by specifying the path to the coverlet.console project.
```bash
dotnet test /p:Coverage=true /p:CoverageExecutablePath="dotnet run -p C:\...\coverlet\src\coverlet.console\coverlet.console.csproj"
```
--------------------------------
### Enable Experimental Feature (Bash/Zsh)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Use this command to enable the COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE feature in Linux/macOS shells. Setting the environment variable to '1' activates the feature.
```bash
# Linux / macOS
export COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE=1
```
--------------------------------
### Enable Exception Logging
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Set this environment variable on Windows to capture detailed exception information during Coverlet.MTP execution.
```shell
set COVERLET_MTP_EXCEPTIONLOG_ENABLED=1
```
--------------------------------
### Running Coverage with `vstest`
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
To use `vstest` for code coverage, the project must be published first. Then, run `vstest` with the `--collect` argument.
```shell
dotnet publish
...
... -> C:\project\bin\Debug\net8.0\testdll.dll
... -> C:\project\bin\Debug\net8.0\publish\
...
dotnet vstest C:\project\bin\Debug\net8.0\publish\testdll.dll --collect:"XPlat Code Coverage"
```
--------------------------------
### Project File Configuration for VSTest Integration
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
Configure your project file to include `coverlet.collector` and `Microsoft.NET.Test.Sdk`. Ensure the target framework is compatible with VSTest integration.
```xml
net8.0;net9.0
false
...
...
```
--------------------------------
### Verify Deterministic Build Output
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/VSTest/DeterministicBuild/HowTo.md
Check for the presence of the CoverletSourceRootsMapping file in your build output directory. This file confirms that the coverage report was generated using deterministic build settings.
```text
\bin\Debug\net8.0\CoverletSourceRootsMapping_XUnitTestProject1
```
--------------------------------
### Use Coverlet Global Tool for Coverage
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Execute coverlet as a global tool to instrument a specific .NET assembly and run tests. This command targets a test DLL, specifies 'dotnet' as the target, and provides arguments for the test execution. The sample output shows instrumented modules, test results, and coverage calculation.
```shell
coverlet "C:\git\coverlet\test\coverlet.core.tests\bin\Debug\net8.0\coverlet.core.tests.dll" --target "dotnet" --targetargs "test C:\git\coverlet\test\coverlet.core.tests --no-build" --verbosity detailed
```
--------------------------------
### Run All Coverage Tests from CLI
Source: https://github.com/coverlet-coverage/coverlet/blob/master/CONTRIBUTING.md
Execute all coverage tests using the dotnet CLI. This command is useful for running tests and generating coverage reports, and it automatically skips tests known to cause issues with Visual Studio.
```bash
dotnet test test/coverlet.core.coverage.tests/
```
--------------------------------
### Run Tests with Coverlet via dotnet exec
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Execute your test assembly using `dotnet exec` and pass the `--coverlet` flag to enable code coverage collection.
```bash
dotnet exec --coverlet
```
--------------------------------
### Configure Local NuGet Feed
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Add a local NuGet.Config file to your test project or solution to point to your locally built Coverlet packages.
```xml
```
--------------------------------
### Merge Coverage from Solution Level
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/MSBuild/MergeWith/HowTo.md
Execute tests from the solution level to collect and merge coverage in a single command. Use sequential execution (`-m:1`) for accurate results, though it may increase test duration. Include `json` format alongside your desired output format.
```bash
dotnet test \
/p:CollectCoverage=true \
/p:CoverletOutput=../CoverageResults/ \
/p:MergeWith="../CoverageResults/coverage.json" \
/p:CoverletOutputFormat="opencover,json" \
-m:1
```
--------------------------------
### Run VSTest with XPlat Code Coverage
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/VSTest/HelloWorld/HowTo.md
Execute VSTest from the XUnitTestProject1 folder to collect code coverage.
```shell
dotnet test --collect:"XPlat Code Coverage"
```
--------------------------------
### Run VSTest with Custom Runsettings
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/VSTest/HelloWorld/HowTo.md
Collect code coverage using VSTest with a custom runsettings file.
```shell
dotnet test --collect:"XPlat Code Coverage" --settings runsettings.xml
```
--------------------------------
### Running Tests with Coverlet via dotnet test
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
Execute tests using the 'dotnet test' command and enable Coverlet code coverage collection with a specified runsettings file.
```shell
dotnet test --collect:"XPlat Code Coverage" --settings coverlet.runsettings
```
--------------------------------
### Package Dependency Map
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.Architecture.md
Visualizes the dependencies between Coverlet packages and their respective integration points.
```mermaid
flowchart TB
CORE["coverlet.core\n(instrumentation + reporting engine)"]
COL["coverlet.collector\n(VSTest integration)"] --> CORE
MSB["coverlet.msbuild\n(MSBuild integration)"] --> CORE
CON["coverlet.console\n(Global tool)"] --> CORE
MTP["coverlet.MTP\n(Microsoft Testing Platform integration)"] --> CORE
VST["VSTest platform"] --> COL
BUILD["MSBuild / dotnet test"] --> MSB
CLI["CLI process wrapper"] --> CON
MTPP["Microsoft.Testing.Platform"] --> MTP
```
--------------------------------
### Instrument Modules Without Local Sources
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
Enable instrumentation for modules that do not have local source files available by setting `InstrumentModulesWithoutLocalSources` to true.
```bash
/p:InstrumentModulesWithoutLocalSources=true
```
--------------------------------
### Run Tests with Deterministic Builds
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Examples/MSBuild/DeterministicBuild/HowTo.md
Execute tests from your test project directory with the specified MSBuild properties to enable coverage collection and deterministic source paths. Do not use the `--no-build` option.
```shell
dotnet test /p:CollectCoverage=true /p:DeterministicSourcePaths=true
```
--------------------------------
### Enable Experimental Feature (PowerShell)
Source: https://github.com/coverlet-coverage/coverlet/blob/master/README.md
Use this command to enable the COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE feature in PowerShell. Setting the environment variable to '1' activates the feature.
```powershell
# Enable
$env:COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE = "1"
# Disable (restore default)
Remove-Item Env:COVERLET_EXPERIMENTAL_AUTOPROP_BACKING_FIELD_CACHE
```
--------------------------------
### Build Local Coverlet Collector Package
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Steps to build a local NuGet package for the Coverlet collector. This is useful for local debugging and development.
```text
C:\git\coverlet\src\coverlet.collector (master -> origin)
λ dotnet pack
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 50,28 ms for C:\git\coverlet\src\coverlet.collector\coverlet.collector.csproj.
Restore completed in 50,28 ms for C:\git\coverlet\src\coverlet.core\coverlet.core.csproj.
coverlet.core -> C:\git\coverlet\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll
coverlet.collector -> C:\git\coverlet\src\coverlet.collector\bin\Debug\netstandard2.0\coverlet.collector.dll
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.8.0.1.nupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.8.0.1.snupkg'.
```
--------------------------------
### Enable Tracker Logging
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Coverlet.MTP.Integration.md
Set this environment variable on Windows to collect detailed logs from the injected coverage tracker. Log files are created near the instrumented module.
```shell
set COVERLET_ENABLETRACKERLOG=1
```
--------------------------------
### Run Coverlet Test Command
Source: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/Troubleshooting.md
Execute the dotnet test command with the 'XPlat Code Coverage' collector to generate coverage reports.
```shell
dotnet test XUnitTestProject1\ --collect:"XPlat Code Coverage"
```
--------------------------------
### Run Performance Tests
Source: https://github.com/coverlet-coverage/coverlet/blob/master/CONTRIBUTING.md
Execute performance tests for hit counting instrumentation. This requires the project to be built and packed first.
```bash
dotnet test /p:CollectCoverage=true test/coverlet.core.performancetest/
```