Edit on GitHub

xUnit1051   v2 v3   Warning

Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken

Cause

A violation of this rule occurs when a method that accepts CancellationToken is not passed a cancellation token.

Reason for rule

To provide for orderly cancellation (especially for tests which have timed out), developers should pass a cancellation token to any method which accepts one. The cancellation token should be TestContext.Current.CancellationToken or a linked token source that includes it.

How to fix violations

To fix a violation of this rule, pass TestContext.Current.CancellationToken.

Examples

Violates

using System.Threading.Tasks;
using Xunit;

public class xUnit1051
{
    [Fact]
    public async ValueTask TestMethod()
    {
        await Task.Delay(1);
    }
}

Does not violate

using System.Threading.Tasks;
using Xunit;

public class xUnit1051
{
    [Fact]
    public async ValueTask TestMethod()
    {
        await Task.Delay(1, TestContext.Current.CancellationToken);
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.