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

RetryAttribute

RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Commands; using System; namespace NUnit.Framework { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class RetryAttribute : PropertyAttribute, IWrapSetUpTearDown, ICommandWrapper { public class RetryCommand : DelegatingTestCommand { private int _tryCount; public RetryCommand(TestCommand innerCommand, int tryCount) : base(innerCommand) { _tryCount = tryCount; } public override TestResult Execute(TestExecutionContext context) { int num = _tryCount; while (num-- > 0) { context.CurrentResult = innerCommand.Execute(context); if (context.CurrentResult.ResultState != ResultState.Failure) break; if (num > 0) context.CurrentResult = context.CurrentTest.MakeTestResult(); } return context.CurrentResult; } } private int _tryCount; public RetryAttribute(int tryCount) : base(tryCount) { _tryCount = tryCount; } public TestCommand Wrap(TestCommand command) { return new RetryCommand(command, _tryCount); } } }