2
votes

I am using NUnit to drive Selenium WebDriver tests via the console runner. Because these are WebDriver tests (and not standard unit tests), tests can fail for external reasons, such as a web page timing out.

I need to be able to RE-RUN any failed tests automatically to see if the test is failing because of a test violation, or just because the test failed due to a timeout.

I already log pass/fail status into a database, and what I want to do is automatically re-run any failed tests, immediately after the original test run. I can do this manually of course, but that takes the fun out of automated testing!

How can I dynamically tell NUnit to run a group of tests in code? (I know how to generate the list/array of failed tests, but don't know how to pass that list or array of tests to NUnit to re-run dynamically.

1

1 Answers

2
votes

I did some research on this earlier. There is a feature requested but won't be out until the NUnit 3.0 is released. I also don't think you can dynamically execute any failed tests without writing custom attribute yourself at this moment. However, there is a project called NUnit retry gives a little bit of flexibility. The Retry attribute can help you out in case the UI tests failure because of external reason. It gives you the option to retry the test n number of time.

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();
}

Taken from here