Repeat
RepeatAttribute is used on a test method to specify that it should be executed multiple times. By default, the test repeats until a failure occurs or all repetitions complete successfully.
Constructors
RepeatAttribute(int count)
RepeatAttribute(int count, bool stopOnFailure)
| Parameter | Type | Description |
|---|---|---|
count |
int |
The number of times to run the test. |
stopOnFailure |
bool |
Whether to stop repeating when a test fails. Default is true. |
Properties
| Property | Type | Description | Default |
|---|---|---|---|
StopOnFailure |
bool |
Whether to stop repeating when a test fails. When false, all repetitions run regardless of failures. |
true |
RequiredPassPercentage |
int |
The minimum percentage of runs (1–100) that must pass for the test to succeed. When set below 100, StopOnFailure is treated as false (and must not be explicitly set to true); iterations run up to the repeat count unless StopWhenOverallResultDetermined is enabled. |
100 |
StopWhenOverallResultDetermined |
bool |
When true and RequiredPassPercentage is below 100, stops iterating as soon as the final outcome is determined — either because the pass threshold is already guaranteed or can no longer be reached. |
false |
Note
The StopOnFailure property was added in NUnit 4.3.0.
Note
The RequiredPassPercentage and StopWhenOverallResultDetermined properties were added in NUnit 5.0.
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ❌ | ❌ |
Examples
Default Behavior (Stop on Failure)
[Test]
[Repeat(5)]
public void TestMethod1()
{
Assert.Pass();
}
Continue on Failure
private int count2;
[Test,Explicit] // Marking the test as Explicit to avoid failing our doc build. You can skip this.
[Repeat(5, StopOnFailure = false)]
public void TestMethod3()
{
count2++;
Assert.That(count2, Is.Not.EqualTo(3)); // Intentional failure on 3rd iteration
}
Pass Threshold
When testing non-deterministic systems (such as LLM-based tests or flaky integrations), you can allow a percentage of runs to fail. The test passes as long as at least RequiredPassPercentage percent of the runs succeed.
// The test passes if at least 80% of the 10 runs succeed.
// Useful for non-deterministic systems where some variation is acceptable.
[Test]
[Repeat(10, RequiredPassPercentage = 80)]
public void NonDeterministicTest()
{
bool result = CallFlakyService();
Assert.That(result, Is.True);
}
private bool CallFlakyService() => true; // placeholder
Early Stop When Outcome Is Determined
When StopWhenOverallResultDetermined is true, NUnit stops iterating as soon as it knows whether the threshold will be met or missed, saving unnecessary test runs.
// Stops as soon as NUnit can guarantee pass or failure:
// - Early success: enough passes accumulated (threshold already guaranteed).
// - Early failure: too many failures (threshold no longer achievable).
[Test]
[Repeat(10, RequiredPassPercentage = 80, StopWhenOverallResultDetermined = true)]
public void NonDeterministicTestWithEarlyStop()
{
bool result = CallFlakyService();
Assert.That(result, Is.True);
}
Notes
- When
StopOnFailureistrue(default), the test stops at the first failure. - When
StopOnFailureisfalse, all repetitions run and all failures are collected. - When
RequiredPassPercentageis set below 100,StopOnFailuremust not be explicitly set totrue— combining the two is a configuration error that producesResultState.Errorwithout running the test. SettingStopOnFailure=falseexplicitly alongside a threshold is valid. WhenStopOnFailureis left at its default (not explicitly set), it is silently treated asfalseso repetitions continue after failures (unlessStopWhenOverallResultDeterminedis enabled). - When
RequiredPassPercentageis below 100 andStopWhenOverallResultDeterminedistrue, NUnit exits the loop as soon as the result is certain:- Early success: the number of passes already guarantees the threshold regardless of the remaining runs.
- Early failure: even if all remaining runs pass, the threshold can no longer be reached.
- If
RepeatAttributeis used on a parameterized method, each individual test case is repeated independently. - It is not currently possible to use
RepeatAttributeon aTestFixtureor any higher level suite. Only test methods may be repeated.