<PackageReference Include="NUnit" Version="4.4.0-beta.1" />

RepeatAttribute

Specifies that a test should be run multiple times.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Commands; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class RepeatAttribute : PropertyAttribute, IRepeatTest, ICommandWrapper { [NullableContext(1)] [Nullable(0)] public class RepeatedTestCommand : DelegatingTestCommand { private readonly int _repeatCount; private readonly bool _stopOnFailure; public RepeatedTestCommand(TestCommand innerCommand, int repeatCount, bool stopOnFailure) : base(innerCommand) { _repeatCount = repeatCount; _stopOnFailure = stopOnFailure; } public override TestResult Execute(TestExecutionContext context) { int num = _repeatCount; TestResult testResult = null; while (num-- > 0) { try { context.CurrentResult = innerCommand.Execute(context); } catch (Exception ex) { if (context.CurrentResult == null) context.CurrentResult = context.CurrentTest.MakeTestResult(); context.CurrentResult.RecordException(ex); } if (context.CurrentResult.ResultState != ResultState.Success) { testResult = context.CurrentResult; if (_stopOnFailure) break; } if (num > 0) { context.CurrentResult = context.CurrentTest.MakeTestResult(); context.CurrentRepeatCount++; } } if (testResult != null) context.CurrentResult = testResult; return context.CurrentResult; } } private readonly int _count; public bool StopOnFailure { get; set; } public RepeatAttribute(int count) : this(count, true) { } public RepeatAttribute(int count, bool stopOnFailure) : base(count) { _count = count; StopOnFailure = stopOnFailure; } [NullableContext(1)] public TestCommand Wrap(TestCommand command) { return new RepeatedTestCommand(command, _count, StopOnFailure); } } }