0

我正在尝试重新运行失败的 Nunit 测试,主要是因为硒的脆弱性。

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

问题是在测试再次运行后,由于原始测试失败,它将继续拆除测试。如何用重试的测试结果覆盖 TestContext.CurrentContext.Result?

4

1 回答 1

0

不幸的RetryAttribute是,它只在 NUnit 3 中可用。从长远来看,它已经存在了很长一段时间,已经在 2015 年实现了。有没有什么原因你不能升级你正在使用的 NUnit 的版本?

如果情况迫使您继续使用如此旧版本(2012 年)的 NUnit,那么实现自己的RetryAttriute. 该定义可以存在于您的测试程序集中并引用您正在使用的 NUnit 版本。

您可以在现有 V2 之后对此类属性进行建模,RepeatAttribute并从 NUnit 3 中获取一些提示RetryAttribute。但是,后者基于一组完全不同的接口,因此未经修改就无法使用。

没有简单的方法可以有效地从TearDown方法重新运行测试,因为在方法完成之前测试不会TearDown结束。实际上修改 NUnit 2.6.2 本身会更容易。

总结一下,按照从易到难的顺序,你可以选择

  1. 升级到 NUnit 3
  2. 添加自定义 RetryAttribute
  3. 修改 NUnit 2.6.2
于 2020-06-02T01:37:06.363 回答