Skip to content

Allow a trailing CancellationToken parameter on MSTest test methods #11986

Description

Summary

Allow MSTest test methods to declare one additional trailing CancellationToken parameter. MSTest supplies TestContext.CancellationToken automatically, so a non-parameterized test can take one parameter and a parameterized test with X data arguments can take X + 1 parameters.

Background and Motivation

Tests currently need access to TestContext just to forward its cancellation token to asynchronous operations. A method parameter makes cooperative cancellation explicit and avoids that boilerplate.

Proposed Feature

[TestMethod]
public async Task TestAsync(CancellationToken cancellationToken)
{
    await RunAsync(cancellationToken);
}

[TestMethod]
[DataRow(42)]
public async Task ParameterizedTestAsync(int value, CancellationToken cancellationToken)
{
    await RunAsync(value, cancellationToken);
}
  • Inject the exact current TestContext.CancellationToken, preserving its existing cancellation and timeout lifetime.
  • Support ordinary methods and data-driven methods, including DataRow and DynamicData.
  • Keep discovery, reflection/source-generated execution, argument validation, and MSTest analyzers consistent.
  • Preserve existing test signatures and data binding; do not inject arbitrary parameters or change lifecycle method signatures.
  • Add regression coverage for token identity, cancellation, data arguments, invalid signatures, and existing behavior.

Alternative Designs

Continue using TestContext.CancellationToken inside the test body. No new attribute or general dependency-injection mechanism is needed for the proposed convenience.

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/mstestMSTest framework (not analyzers or assertions).area/parameterized-testsDataRow / DynamicData / parameterized tests.needs/triageNeeds triage by a maintainer.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions