简体   繁体   English

如何重新运行失败的 Nunit 测试 2.6.2

[英]How to re-run a fail Nunit Test 2.6.2

I am trying to Re-run failed Nunit tests, mainly because of flackiness with selenium.我正在尝试重新运行失败的 Nunit 测试,主要是因为 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?如何用重试的测试结果覆盖 TestContext.CurrentContext.Result?

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?不幸的是, RetryAttribute仅在 NUnit 3 中可用。从长远来看,它已经存在了很长一段时间,已经在 2015 年实现了。是否有无法升级您正在使用的 NUnit 版本的原因?

If circumstances force you to keep using such an old version (2012) of NUnit, it would not be difficult to implement your own RetryAttriute .如果情况迫使您继续使用这样一个旧版本(2012 年)的 NUnit,那么实现自己的RetryAttriute并不难。 The definition could live in your test assembly itself and reference the version of NUnit you are using.该定义可以存在于您的测试程序集中并引用您正在使用的 NUnit 版本。

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.您可以在现有的 V2 RepeatAttribute之后使用 model 这样的属性,并且还可以从 NUnit 3 中的RetryAttribute中获得一些提示。但是,后者基于一组完全不同的接口,因此无法在不修改的情况下使用。

There is no easy way to effectively rerun tests from a TearDown method because the test isn't over until the TearDown method completes.没有简单的方法可以有效地从TearDown方法重新运行测试,因为在TearDown方法完成之前测试不会结束。 It would actually be easier to modify NUnit 2.6.2 itself.实际上修改 NUnit 2.6.2 本身会更容易。

To sum up, in order of easiest to most difficult, you can choose总结一下,按照从易到难的顺序,你可以选择

  1. To upgrade to NUnit 3升级到 NUnit 3
  2. To add a custom RetryAttribute添加自定义 RetryAttribute
  3. To modify NUnit 2.6.2修改 NUnit 2.6.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM