Skip to content

Public API for coverage extensions: threshold checks, terminal summary, and exit code in Microsoft.Testing.Platform #9943

Closed

Description

Summary

Add a public API in Microsoft.Testing.Platform (MTP) so coverage extensions can perform coverage-threshold checks and surface coverage results in the terminal summary and threshold checks in the process exit code. Adds two message-bus types, three enums, and a new exit code.

Proposed in PR #9896 — filing here so the team can approve the public API shape before it ships (MTP public API is effectively permanent).

Background and Motivation

Today a coverage extension cannot:

  • Enforce a coverage threshold — there's no platform contract to report "coverage X% is below the required Y%", and no way to fail the run for it.
  • Show coverage in the standard terminal summary — the built-in terminal reporter has no coverage concept, so a coverage tool must print its own ad-hoc output.
  • Influence the process exit code — there's no dedicated, ignorable exit code for a threshold failure, so each tool invents its own convention.

Coverage is produced out-of-process: a coverage extension registers as an ITestHostProcessLifetimeHandler (runs in the test-host controller) and publishes these messages after the test host exits; the controller then renders the summary and applies the verdict. A stable public message contract is what lets an external coverage extension plug into that pipeline.

Example coverage extensions that would benefit from this change:

Proposed Feature

New public API in Microsoft.Testing.Platform.Extensions.Messages:

public enum CoverageType { Line = 0, Branch = 1, Method = 2 }
public enum CoverageThresholdStatus { Passed = 0, Failed = 1 }
public enum CoverageThresholdStatistic { Minimum = 0, Total = 1, Average = 2 }

public sealed class TestCoverageMessage : PropertyBagData
{
    public TestCoverageMessage(string moduleName, double value, CoverageType coverageType);
    public string ModuleName { get; }
    public double Value { get; }          // percentage, 0..100
    public CoverageType CoverageType { get; }
}

public sealed class TestCoverageThresholdMessage : PropertyBagData
{
    public TestCoverageThresholdMessage(double value, double threshold,
        CoverageType coverageType, CoverageThresholdStatus status,
        CoverageThresholdStatistic statistic);
    public double Value { get; }
    public double Threshold { get; }
    public CoverageType CoverageType { get; }
    public CoverageThresholdStatus Status { get; }
    public CoverageThresholdStatistic Statistic { get; }
}

Plus a new exit code Microsoft.Testing.Platform.Helpers.ExitCode.CoverageThresholdFailed = 14.

Behavior: coverage extensions publish these messages; the terminal device renders a coverage summary + threshold results, and a failed threshold on an otherwise-successful run sets exit code 14, routed through the existing --ignore-exit-code policy so --ignore-exit-code 14 suppresses it.

Alternative Designs

  • No platform API (coverage extension owns everything). Rejected — a tool can't integrate with the built-in terminal reporter or --ignore-exit-code, and every tool diverges on output and exit codes.
  • In-process coverage production. Rejected — collectors run in the controller around the test-host process, so the out-of-process model is the realistic one.
  • Reuse a generic/combined message. Rejected — coverage results and threshold verdicts are distinct, independently produced concepts that benefit from strongly-typed contracts.

Activity

  1. Evangelink commented on Jul 14, 2026

    @Evangelink
    Member

    Thanks for opening the tracking issue James Newell (@james-newell-forge)!

    Picking up the API-shape discussion from #9896 — before we lock in a permanent public surface, I'd like to suggest reconsidering the message types, in line with the review feedback there:

    • Base type. TestCoverageMessage and TestCoverageThresholdMessage both derive from PropertyBagData but never populate the bag, so a consumer inspecting the bag generically sees nothing. I'd suggest we either carry the coverage data as actual bag properties, or move these to a base type that better fits a strongly-typed contract instead of an always-empty PropertyBagData. My preference leans toward a dedicated base for clarity.
    • Naming. Since the API is effectively permanent, let's make sure the members read well (e.g. Statistic rather than any abbreviation, and unambiguous enum names). The issue text already uses Statistic/CoverageThresholdStatistic, which is the direction I'd want.

    To be clear, this is still open for discussion — I'd like the team (and you) to weigh in on the base-type choice in particular before more implementation effort goes in. What are your thoughts?

  2. james-newell-forge commented on Jul 16, 2026

    @james-newell-forge
    ContributorAuthor

    Base type. TestCoverageMessage and TestCoverageThresholdMessage both derive from PropertyBagData but never populate the bag, so a consumer inspecting the bag generically sees nothing. I'd suggest we either carry the coverage data as actual bag properties, or move these to a base type that better fits a strongly-typed contract instead of an always-empty PropertyBagData. My preference leans toward a dedicated base for clarity.

    That is a good point. I wasn't sure if it was standard for all messages to derive from PropertyBagData or not. I created a new base class TestCoverageData for these messages to inherit.

  3. james-newell-forge commented on Jul 16, 2026

    @james-newell-forge
    ContributorAuthor

    Naming. Since the API is effectively permanent, let's make sure the members read well (e.g. Statistic rather than any abbreviation, and unambiguous enum names). The issue text already uses Statistic/CoverageThresholdStatistic, which is the direction I'd want.

    Yeah I kind of borrowed these names from coverlet. I made an update just now that hopefully makes the names more descriptive.

    • Value -> CoveragePercentage
    • CoverageType -> CoverageMetric
    • Threshold -> ThresholdPercentage

    Also removed CoverageThresholdStatus and made it a computed a bool.

  4. james-newell-forge commented on Jul 16, 2026

    @james-newell-forge
    ContributorAuthor

    I just noticed there is an RFC for this change. Let me take a look, this is the first time I was aware of it.

  5. james-newell-forge commented on Jul 16, 2026

    @james-newell-forge
    ContributorAuthor

    Amaury Levé (@Evangelink) ok i see what you were saying on holding off on the implementation part 😄

  6. Evangelink commented on Jul 16, 2026

    @Evangelink
    Member

    James Newell (@james-newell-forge) it's ok! I want to move forward with your PR! I did a PR to your PR (on your fork) to match the RFC (feel free to review and comment things that sound off to you)

  7. added
    area/mtp
    area/mtp-extensions
    and removed
    needs/triage
    on Jul 24, 2026
  8. james-newell-forge commented on Jul 24, 2026

    @james-newell-forge
    ContributorAuthor

    Amaury Levé (@Evangelink) Thank you for getting this over the finish line. I apologize I wasn't much help at end, I was offline for most of this week. Thanks again. I think this will feature will be a great addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/mtparea/mtp-extensions

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions