Allow a trailing CancellationToken parameter on MSTest test methods

Cerrado
#11,986 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@Evangelink ya está trabajando en esto.

Desde el 16/10/2026.

  • #11991 de @Evangelink — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
22/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
csharp
Área
testing

Línea de trabajo

The proposal touches MSTest test discovery, execution (both reflection and source-generated paths), argument binding for DataRow and DynamicData, and the MSTest analyzers, so start by tracing how TestContext.CancellationToken is currently made available to a test body. Done means a test method with a trailing CancellationToken parameter receives the same token as TestContext, data-driven methods bind their data arguments unchanged, and invalid signatures are rejected consistently across discovery, execution and analyzers. The issue has no assignee or pull request yet, but the design spans several subsystems, so confirm the approach with maintainers before writing code.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area/mstest area/parameterized-tests needs/triage

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.

Lenguaje dominante
C#
Estrellas
1.1k
Forks
312
Merge medio
10 h 39 min
PR fusionados (30 d)
349

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/testfx

Todos los issues de microsoft/testfx

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.