diff --git a/docs/core/testing/mstest-analyzers/mstest0045.md b/docs/core/testing/mstest-analyzers/mstest0045.md index 2f9bfb024cee8..bb42d89bb9635 100644 --- a/docs/core/testing/mstest-analyzers/mstest0045.md +++ b/docs/core/testing/mstest-analyzers/mstest0045.md @@ -30,6 +30,11 @@ A test method uses , you should set `CooperativeCancellation` to `true` to enable cooperative cancellation. Without cooperative cancellation, the test framework stops observing the test execution when the timeout is reached, but the test continues running in the background. This can lead to problems for other tests or cleanup steps, as the original test is still executing and might interfere with subsequent operations. When using cooperative cancellation mode, MSTest only triggers cancellation of the token and you're responsible for flowing and utilizing the test context token in your test code. This mode aligns with the default behavior of cancellation in .NET. @@ -38,6 +43,21 @@ When using cooperative cancellation mode, MSTest only triggers cancellation of t Use the provided code fixer to automatically set the `CooperativeCancellation` property to `true` on the . You can also manually add the property if needed. +```csharp +[TestClass] +public class TestClass1 +{ + public TestContext TestContext { get; set; } + + [Timeout(TimeoutValue, CooperativeCancellation = true)] + [TestMethod] + public void TestMethod1() + { + // Respect TestContext.CancellationTokenSource.Token + } +} +``` + Alternatively, you can configure cooperative cancellation globally in your [runsettings](/dotnet/core/testing/unit-testing-mstest-configure#mstest-element) or [testconfig.json](/dotnet/core/testing/unit-testing-mstest-configure#timeout-settings) file to apply this setting to all timeout attributes in your test project. ## When to suppress warnings