Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
| Property | Value |
|---|---|
| Rule ID | MSTEST0054 |
| Title | Use TestContext.CancellationToken instead of TestContext.CancellationTokenSource.Token |
| Category | Usage |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default | Yes |
| Default severity | Info |
| Introduced in version | 3.11.0 |
| Is there a code fix | Yes |
Cause
Accessing CancellationToken via TestContext.CancellationTokenSource.Token instead of using the TestContext.CancellationToken property.
Rule description
MSTest controls cancellation of the test's execution. To let an operation observe that cancellation, pass TestContext.CancellationToken directly instead of accessing the framework's CancellationTokenSource.
A CancellationToken lets your code observe cancellation without exposing the source's cancellation and disposal operations. The direct property expresses that ownership boundary and avoids a dependency on the older source property when all you need is its token.
How to fix violations
Use the TestContext.CancellationToken property instead of TestContext.CancellationTokenSource.Token.
For example, change this:
public TestContext TestContext { get; set; }
[TestMethod]
public async Task TestMethod()
{
await Task.Delay(1000, TestContext.CancellationTokenSource.Token);
}
To this:
public TestContext TestContext { get; set; }
[TestMethod]
public async Task TestMethod()
{
await Task.Delay(1000, TestContext.CancellationToken);
}
When to suppress warnings
Don't suppress warnings from this rule. The use of CancellationTokenSource property is not recommended and might be removed in a future release.