Skip to content

Commit 473bcc4

Browse files
authored
Merge pull request #1071 from nunit/repeatAttribute
Updated doc on repeat attribute
2 parents ee62a00 + 2006eb9 commit 473bcc4

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# Repeat
22

33
**RepeatAttribute** is used on a test method to specify that it should be
4-
executed multiple times. If any repetition fails, the remaining ones are
5-
not run and a failure is reported.
4+
executed multiple times.
65

7-
## Notes
6+
By default, the test is repeated until a failure occurs. If no failures occur, it runs for the specified number of repetitions.
87

9-
1. If RepeatAttribute is used on a parameterized method, each individual
10-
test case created for that method is repeated.
11-
2. It is not currently possible to use RepeatAttribute on a TestFixture
12-
or any higher level suite. Only test cases may be repeated.
8+
You can change this behavior in case of a failure, so that it continues to run after the failure by setting the property `StopOnFailure` to `false`. (From version 4.3.0)
139

1410
## Examples
1511

16-
```csharp
17-
[Test]
18-
[Repeat(25)]
19-
public void MyTest()
20-
{
21-
/* The contents of this test will be run 25 times. */
22-
}
23-
```
12+
### The default behavior
13+
14+
[!code-csharp[RepeatDefaultAttributeExample](~/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs#RepeatDefaultAttributeExample)]
15+
16+
### Run all regardless of failures
17+
18+
[!code-csharp[RepeatWithFaultAttributeExample](~/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs#RepeatWithFaultAttributeExample)]
19+
20+
> [!WARNING]
21+
> There is currently (as of 4.4.0) a [bug](https://github.com/nunit/nunit/issues/5031) which causes only the last successful console statement to be output. Also, in case of failures, only the latest failure is shown.
22+
> [!NOTE]
23+
> If RepeatAttribute is used on a parameterized method,
24+
> each individual test case created for that method is repeated.
25+
> It is not currently possible to use RepeatAttribute on a TestFixture or any higher level suite. Only test cases may be repeated.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
8+
namespace Snippets.NUnit.Attributes;
9+
10+
11+
public class RepeatAttributeExample
12+
{
13+
#region RepeatDefaultAttributeExample
14+
[Test]
15+
[Repeat(5)]
16+
public void TestMethod1()
17+
{
18+
Assert.Pass();
19+
}
20+
#endregion
21+
22+
private int count1 = 0;
23+
#region RepeatWithFaultAndOutputAttributeExample
24+
[Test, Explicit("Issue 5031 must be fixed")]
25+
[Repeat(5, StopOnFailure = false)]
26+
public void TestMethod2()
27+
{
28+
29+
TestContext.Out.WriteLine(count1);
30+
count1++;
31+
Assert.That(count1, Is.Not.EqualTo(3).And.Not.EqualTo(4));
32+
}
33+
#endregion
34+
35+
#region RepeatWithFaultAttributeExample
36+
37+
private int count2 = 0;
38+
39+
[Test,Explicit] // Marking the test as Explicit to avoid failing our doc build. You can skip this.
40+
[Repeat(5, StopOnFailure = false)]
41+
public void TestMethod3()
42+
{
43+
count2++;
44+
Assert.That(count2, Is.Not.EqualTo(3)); // Intentional failure on 3rd iteration
45+
}
46+
#endregion
47+
}

0 commit comments

Comments
 (0)