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 |
Note
The StopOnFailure property was added in NUnit 4.3.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
}
Notes
- When
StopOnFailure is true (default), the test stops at the first failure.
- When
StopOnFailure is false, all repetitions run and all failures are collected.
- If
RepeatAttribute is used on a parameterized method, each individual test case is repeated.
- It is not currently possible to use
RepeatAttribute on a TestFixture or any higher level suite. Only test methods may be repeated.
See Also