Search code examples
c#seleniumnunitappium

Nunit_Retry runs only ones on timeout failure


I'm using retry in order to run my tests multiple time when it fails. But, I have noticed that when there's an exception failure (Timeout when waiting for an elemnt e.g), then the test will run only ones and not as configured.

This is what I'm trying:

 [Test]
[NUnit_retry.Retry(times: 3, requiredPassCount: 1)]
        public void MyTest()
     {
        some code...
     }
        
 


Solution

  • NUnit's [Retry] attribute only retries when a test fails, rather than errors. This means that AssertionExceptions will trigger a retry, however other exceptions will not.

    You don't say how you are doing your timeouts - are you using the NUnit [Timeout] attribute for it? This should trigger the Retry. If you're timeout is causing another kind of exception however, you may need to expect this, and instead 'assert it doesn't happen'. You could do this be asserting the relevant line doesn't throw any exception e.g.

    string toFetch;
    Assert.That(() => { toFetch = FetchMyString(); }, Throws.Nothing);
    

    If you wanted to instead do something globally, you may instead wish to implement a custom attribute which retries on ResultState.Error, as well as ResultState.Failure. Have a look at how the current RetryAttribute is implemented, which may give you an idea of where to start with that one.

    Retry Attribute Docs