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

MaxTimeCommand

MaxTimeCommand adjusts the result of a successful test to a failure if the elapsed time has exceeded the specified maximum time allowed.
using NUnit.Framework.Interfaces; using System.Diagnostics; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal.Commands { public class MaxTimeCommand : AfterTestCommand { [NullableContext(1)] public MaxTimeCommand(TestCommand innerCommand, int maxTime) : base(innerCommand) { AfterTest = delegate(TestExecutionContext context) { long num = Stopwatch.GetTimestamp() - context.StartTicks; double duration = (double)num / (double)Stopwatch.Frequency; TestResult currentResult = context.CurrentResult; currentResult.Duration = duration; if (currentResult.ResultState == ResultState.Success) { double num2 = currentResult.Duration * 1000; if (num2 > (double)maxTime) { TestResult testResult = currentResult; ResultState failure = ResultState.Failure; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(40, 2); defaultInterpolatedStringHandler.AppendLiteral("Elapsed time of "); defaultInterpolatedStringHandler.AppendFormatted(num2); defaultInterpolatedStringHandler.AppendLiteral("ms exceeds maximum of "); defaultInterpolatedStringHandler.AppendFormatted(maxTime); defaultInterpolatedStringHandler.AppendLiteral("ms"); testResult.SetResult(failure, defaultInterpolatedStringHandler.ToStringAndClear()); } } }; } } }