Ayush Goel
Ayush Goel

Reputation: 54

Retry in nunit not working when test times out

I am using Timeout attribute for nunit test case as below:

[Test, TestCaseSource("TestCases"), Retry(2), Timeout(10000)
public void test(){
    // Some code that runs for more than 10 seconds
}

I have gone through the documentation of nunit but it said that apart from assertion error, retry will not work but I have a situation where test times out.

I want this test to execute again as it is timing out but it executes only once using above code. Please help.

Upvotes: 0

Views: 1231

Answers (1)

Matthew Wherry
Matthew Wherry

Reputation: 353

We had the same issue (E2E UI testing, which is finicky so test throws exceptions and retry doesnt work)

You can do a workaround and wrap your test code IE

protected void ExecuteTest(Action test)
{
  try
  {
    test();
  }
  catch (Exception ex)
  {
    //If the caught exception is not an assert exception but an unhandled exception.
    if (!(ex is AssertionException))
      Assert.Fail(ex.Message);
  }
}

Which for a test youd want to retry even if it throws would look like

[Test, Retry(3)]
public void TestCase()
{
  ExecuteTest(() =>{
    <test code>
  });
}

Im unsure how the nunit timeout attribute would work (Im assuming the test() call would just throw a timeout exception in which case this solution would work) but it this solution doesnt work for that you can switch to a task or action and WaitOne or something and have a default param for execute test be 1000 for your timeout IE

 protected void ExecuteTest(Action test, int timeoutSeconds = 10)
{
  try
  {
    var task = Task.Run(test);
    if (!task.Wait(TimeSpan.FromSeconds(timeoutSeconds)))
      throw new TimeoutException("Timed out");
    test.BeginInvoke(null,null);
  }
  catch (Exception ex)
  {
    //If the caught exception is not an assert exception but an unhandled exception.
    if (!(ex is AssertionException))
      Assert.Fail(ex.Message);
  }
}

This looked like our best solution so it's what we currently have implemented and seems to work fine

Upvotes: 2

Related Questions