0

I am trying to Re-run failed Nunit tests, mainly because of flackiness with selenium.

    [TearDown]
    public virtual void TearDown()
    {
        var testName = TestContext.CurrentContext.Test.FullName.Replace("Server.Tests.", string.Empty);

        if (TestContext.CurrentContext.Result.Status == TestStatus.Passed)
            return;
        else if (_testFailure < 3) {
            _testFailure++;
            Console.WriteLine($"\n {testName} {TestContext.CurrentContext.Result.Status}... Retrying attempt {_testFailure}");
            DbReloader.LoadUnitTestData(DbFactory);
            TestExecutionContext.CurrentContext.CurrentTest.Run(new NullListener(), TestFilter.Empty);

        }
        BrowserDriver.GetScreenshot()
                     .SaveAsFile($"{testName}.fail.png", ImageFormat.Png);
    }

The problem is after the test runs again, it will continue with the tear down of the test as the original test failed. How do I override the TestContext.CurrentContext.Result with my retried test results?

Kuzon
  • 782
  • 5
  • 20
  • 49

1 Answers1

0

Unfortunately, the RetryAttribute is only available in NUnit 3. To put things into perspective, it has been around quite a while, having been implemented in 2015. Are there reasons you can't upgrade the version of NUnit you are using?

If circumstances force you to keep using such an old version (2012) of NUnit, it would not be difficult to implement your own RetryAttriute. The definition could live in your test assembly itself and reference the version of NUnit you are using.

You could model such an attribute after the existing V2 RepeatAttribute and also take some hints from the RetryAttribute in NUnit 3. The latter, however, is based on an entirely different set of interfaces so can't be used without modification.

There is no easy way to effectively rerun tests from a TearDown method because the test isn't over until the TearDown method completes. It would actually be easier to modify NUnit 2.6.2 itself.

To sum up, in order of easiest to most difficult, you can choose

  1. To upgrade to NUnit 3
  2. To add a custom RetryAttribute
  3. To modify NUnit 2.6.2
Charlie
  • 12,928
  • 1
  • 27
  • 31