Codebelt
v10.0.1

Codebelt.Coverlet.MTP

Collect code coverage in MTP test projects without flattening mixed-target dependency graphs.

.NET 10.0 / .NET 9.0 / .NET Standard 2.0 MIT 1,374 downloads

Overview

Codebelt.Coverlet.MTP packages Coverlet's Microsoft Testing Platform integration as one NuGet package with explicit assets for netstandard2.0, net9.0, and net10.0. It keeps the implementation assembly name coverlet.MTP.dll and the Coverlet.MTP.TestingPlatformBuilderHook discovery contract, so MTP still finds the extension the usual way while NuGet resolves framework-aligned dependency groups for the active target.

The value is not a new coverage engine. The package still brings coverlet.core and the embedded tracker template with it, then layers MTP-specific registration, command-line options, configuration parsing, and report emission around that engine. By default the integration emits json and cobertura reports, and it keeps the familiar --coverlet, --coverlet-output-format, --coverlet-exclude*, and threshold option family.

Key APIs

TestingPlatformBuilderHook is the NuGet-facing registration seam. The package's build-transitive props file points Microsoft Testing Platform at this type so coverage support is registered automatically when the package is referenced.

CoverletExtensionProvider.AddCoverletExtensionProvider(this ITestApplicationBuilder builder, bool ignoreIfNotSupported = false) is the explicit registration path. It wires the controller-side environment variable provider, pre-start instrumentation handler, in-process lifetime handler, and command-line provider into an existing MTP builder.

CoverletConfigurationExtensions.AddCoverletCodeCoverage(this ITestApplicationBuilder builder) is the smaller helper that registers the test-session lifetime handler directly when a test application builder only needs the in-process coverage hook.

CoverletMTPSettings is the configuration model behind testconfig.json and coverlet.mtp.appsettings.json. It carries output formats, include and exclude filters, threshold settings, ExcludeAssembliesWithoutSources, and related instrumentation switches.

CoverletTestSessionHandler is the in-process session-lifetime component that flushes tracker data when the test session finishes. It is one half of the package's controller/test-host split, with the controller-side instrumentation handled through the extension provider.

Basic usage

A multi-targeted MTP test project can reference one package while NuGet resolves framework-aligned assets for each runnable target:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net9.0;net10.0</TargetFrameworks>
    <OutputType>Exe</OutputType>
    <UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit.v3.mtp-v2" Version="4.0.1" />
    <PackageReference Include="Microsoft.Testing.Platform" Version="2.4.1" />
    <PackageReference Include="Codebelt.Coverlet.MTP" Version="10.0.1" />
  </ItemGroup>
</Project>

Run coverage with the normal MTP entry point:

dotnet test --coverlet

If the test project needs a declared coverage contract instead of command-line defaults, add a testconfig.json file and let the package read its platformOptions.Coverlet section.

Installation

dotnet add package Codebelt.Coverlet.MTP

Usage guidance

Choose this package when the test suite is already on Microsoft Testing Platform and especially when the solution targets more than one framework. That packaging detail matters more than it first appears: coverage tooling sits in the restore, build, and test path, so aligning dependency groups to each TFM helps mixed-target repositories avoid subtle restore and startup friction.

Prefer testconfig.json when you want coverage settings to be explicit and reproducible. In that mode, the configuration file becomes authoritative instead of being merged with convenience defaults. Stay with command-line switches when you want the built-in defaults and dynamic test-assembly exclusions.

Treat netstandard2.0 as the compatibility floor, not the primary executable host. The repository validates the runnable test-host scenarios on net9.0 and net10.0. If you need non-MTP Coverlet drivers, use the upstream Coverlet packages rather than forcing this narrower package into a broader role.