I have a suite of NUnit tests, some of which fail intermittently, probably because of timing problems. I'd like to find these flaky unit tests. Is there a way to repeat each test multiple times without having to put a Repeat() attribute on each test? We routinely use the resharper and ncrunch runners, but also have access to the nunit gui and console runners.
Asked
Active
Viewed 7,669 times
2
-
You may be doing too much in each unit test - they should only contain a single test for one single part of your system. Are you able to add the code for the suspect unit test to your question? – Piers Myers Oct 06 '14 at 10:58
-
Hi Piers. I'm happy that the tests are not overly complicated. More that, the tests inherently have a timing dependency built in (using DateTime calculations, and timers etc). They pass most of the time, and fail unpredictably and sporadically. – Phillip Ngan Oct 06 '14 at 19:12
1 Answers
3
NUnit 3
In NUnit 3, you may use Retry attribute:
RetryAttribute
is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times.Notes:
It is not currently possible to use
RetryAttribute
on aTestFixture
or any other type of test suite. Only single tests may be repeated.If a test has an unexpected exception, an error result is returned and it is not retried. Only assertion failures can trigger a retry. To convert an unexpected exception into an assertion failure, see the
ThrowsConstraint
.
NUnit 2
NUnit 2 doesn't support retries, but you may use NUnit-retry plug-in (NuGet, GitHub). An example of use:
private static int run = 0;
...
[Test]
[Retry(Times = 3, RequiredPassCount = 2)]
public void One_Failure_On_Three_Should_Pass()
{
run++;
if (run == 1)
{
Assert.Fail();
}
Assert.Pass();
}
See also
- Feature - Add 'Retry Attribute' to repeat test upon failure. Discussion about the feature on Launchpad

Dariusz Woźniak
- 9,640
- 6
- 60
- 73
-
2@Daruisz I have over 6000 unit tests. Is there a centralized way of running all the tests multiple times? – Phillip Ngan Oct 09 '14 at 04:21
-
@PhillipNgan: NUnit-retry allows to put Retry attribute on either class or method, so the answer is – it's not possible to do that by using this tool. – Dariusz Woźniak Oct 09 '14 at 10:40
-
But if it can be applied to a class then that *is* better than the standard NUnit, which only allows [Repeat] at test method level. – demoncodemonkey Oct 09 '14 at 14:12
-
Unfortunately I couldn't get it to work (couldn't figure out the installation instructions!) – demoncodemonkey Oct 09 '14 at 14:19