Skip to content

feat: make [Timeout] CancellationToken parameter optional (warning instead of error) #4767

@thomhurst

Description

@thomhurst

Problem

Every method with `[Timeout]` must include a `CancellationToken` parameter, or it's a hard compile error (TUnit0015):

```csharp
// Error TUnit0015: Missing TimeoutAttribute cancellation token parameter
[Test, Timeout(30_000)]
public async Task Step3_Worker_Fulfills_Order()
{
// Manual polling loop - doesn't use cancellation token at all
while (DateTime.UtcNow < deadline)
{
order = await Client.GetFromJsonAsync($"/api/orders/{id}");
if (order?.Status == OrderStatus.Fulfilled) break;
await Task.Delay(500);
}
}
```

In integration tests, `[Timeout]` is often used as a safety net to prevent tests from hanging indefinitely. The test logic itself doesn't always pass the cancellation token to anything meaningful - it's just there to add a ceiling.

Proposed Change

Make TUnit0015 a warning instead of an error. The timeout would still work (TUnit cancels the test externally), but the method wouldn't be forced to accept an unused parameter.

Users who want cooperative cancellation can still add the parameter (and the warning encourages it), but it wouldn't block compilation.

Alternatively, provide a way to suppress just this diagnostic per-method.

Context

Discovered while building the CloudShop Aspire + TUnit example (#4761). 5 out of 5 methods with `[Timeout]` had unused `CancellationToken` parameters added just to satisfy the compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions