Allow a trailing CancellationToken parameter on MSTest test methods
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 22/100
Research direction
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.
Written by the indexing model from the issue text.
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
DataRowandDynamicData. - 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.
- Dominant language
- C#
- Stars
- 1k
- Forks
- 314
- Avg merge
- 9h 33m
- Merged PRs (30d)
- 427
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/testfx
-
type/automation type/tech-debt
Difficulty 4/5 3-5 days Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
area/dump needs/triage
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
microsoft/testfx#11981 · 2 comments ·
Maintainers usually reply within 1 day
-
agentic-workflows type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
type/automation type/tech-debt
Difficulty 5/5 Over a week Newbie friendliness 5/100
Maintainers usually reply within 1 day
-
[msbuild-quality] MSBuild File Quality Report — 2026-10-15: missing FileWrites in CopyMSTestV2Resources targetsPossibly taken @YuliiaKovalova claimed this today. Openarea/mtp-msbuild type/automation type/tech-debt
microsoft/testfx#11937 · 1 comment · 1 reaction · 2 assignees ·
Maintainers usually reply within 1 day
All issues in microsoft/testfx
Similar issues
-
area/indexing bug package/core triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
enhancement needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
elastic/docs-builder#4495 ·
Maintainers usually reply within 1 day
-
ASP.NET Core container quickstart: `docker-run: debug` task sample is missing its closing braceOpen
Difficulty 1/5 Under an hour Newbie friendliness 88/100
microsoft/vscode-docs#10499 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
SourceDotNET/Source.NET#203 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day