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

TimeoutCommand

TimeoutCommand creates a timer in order to cancel a test if it exceeds a specified time and adjusts the test result if it did time out.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal.Abstractions; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal.Commands { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class TimeoutCommand : BeforeAndAfterTestCommand { private readonly int _timeout; private readonly IDebugger _debugger; internal TimeoutCommand(TestCommand innerCommand, int timeout, IDebugger debugger) : base(innerCommand) { _timeout = timeout; _debugger = debugger; Guard.ArgumentValid(innerCommand.Test is TestMethod, "TimeoutCommand may only apply to a TestMethod", "innerCommand"); Guard.ArgumentValid(timeout > 0, "Timeout value must be greater than zero", "timeout"); ArgumentNullException.ThrowIfNull(debugger, "debugger"); BeforeTest = delegate { }; AfterTest = delegate { }; } public override TestResult Execute(TestExecutionContext context) { context.CurrentResult.SetResult(ResultState.Error, "TargetFramework doesn't support timeout on tests."); return context.CurrentResult; } } }