<PackageReference Include="NUnit" Version="4.2.2" />

CancelAfterAttribute

Applies a timeout in milliseconds to a test. When applied to a method, the test's cancellation token is cancelled if the timeout is exceeded.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class CancelAfterAttribute : PropertyAttribute, IApplyToContext { private readonly int _timeout; public CancelAfterAttribute(int timeout) : base("Timeout", timeout) { _timeout = timeout; base.Properties.Add("UseCancellation", true); } [System.Runtime.CompilerServices.NullableContext(1)] void IApplyToContext.ApplyToContext(TestExecutionContext context) { context.TestCaseTimeout = _timeout; context.UseCancellation = true; } } }