【问题标题】:Failing an NUnit test inside teardown在拆解中未通过 NUnit 测试
【发布时间】:2015-12-22 00:52:11
【问题描述】:

我有一堆使用 selenium 和 NUnit 运行的自动化 UI 测试。

每次 nunit 测试后,我都想检查浏览器是否发生了任何 JS 错误。如果有任何 JS 错误,导致它们的测试应该失败。我希望它对我编写的所有测试都运行,而不必将检查复制到每个测试中。

我也会对任何失败进行截图。

    [TearDown]
    public void TearDown()
    {
        Assert.Fail("Some JS error occurred"); //This is to simulate a JS error assertion

        if (TestContext.CurrentContext.Result.Status != TestStatus.Passed)
        {
            Driver.TakeScreenshot(TestContext.CurrentContext.Test.Name, "Failed");
        }
    }

如果我在 teardown 中的断言失败,它将永远不会执行屏幕截图代码(因为断言是一个异常)。

这里有没有更好的方法让测试失败,以便我可以继续我的逻辑?

【问题讨论】:

  • 你为什么要调用失败断言?
  • @steve "每次测试后我都想检查浏览器是否发生了任何 JS 错误,如果有任何失败的测试" 我会稍微澄清一下代码
  • @drets 现在读起来更清楚了吗?
  • @LukeMcGregor 如果您的问题仅与屏幕截图有关,您可以不使用Assert.Fail 而是使用标志。例如。 jsErrorsOccured 并像下面这样使用它:if (jsErrorsOccured || (TestContext.CurrentContext.Result.Status != TestStatus.Passed)) { ... } ?
  • @LukeMcGregor 辛苦了:simple-talk.com/dotnet/.net-tools/…:P

标签: c# selenium nunit


【解决方案1】:

您可以扩展自己的 TestActionAttribute 来运行您的 JS 测试。如果在AfterTest() 中抛出断言失败,NUnit 似乎会报告测试失败。

例如这个:

[CheckForJSErrors] // your custom attribute - applies to all tests
public class ClassTest
{
    [Test]
    public void MyTest()
    {
        // Your test case
    }
}

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class CheckForJSErrorsAttribute : TestActionAttribute
{
    public override void AfterTest(TestDetails testDetails)
    {
        // if you only want to check for JS errors if the test passed, then:
        if(TestContext.CurrentContext.Result.Status == TestStatus.Passed)
        {
            Driver.TakeScreenshot(testDetails.FullName, "Failed");
            Assert.Fail("Some JS error occurred"); //This is to simulate a JS error assertion
        }
    }
    public override ActionTargets Targets
    {
        // explicitly says to run this after each test, even if attr is defined on the entire test fixture
        get { return ActionTargets.Test; }
    }
}

在 NUnit 中产生此故障:

SO_34306757.ClassTest.MyTest: TearDown : NUnit.Framework.AssertionException : Some JS error occurred

我已经为整个测试夹具声明了[CheckForJSErrors] 属性以避免必须在每个测试用例上声明它,但如果你愿意,你可以按测试方法声明它。

【讨论】:

  • 这是一个很好的检查方法,我喜欢它胜过基类。但我只想在测试失败或检测到 JS 错误时截屏
【解决方案2】:

如果您想在每次测试后检查相同的内容,您可以选择一个在每个测试本身中调用的通用方法。这样一来,您就不会复制测试代码,并且不会破坏失败断言。

例如。

[Test]
public void Test1()
{
    // sometest code
    TestJS();
}

[Test]
public void Test2()
{
    // some other test code
    TestJS();
}

public void TestJS()
{
    Assert.Fail("Or whatever to test the JS");
}

[TearDown]
public void TearDown()
{
    if (TestContext.CurrentContext.Result.Status != TestStatus.Passed)
    {
        Driver.TakeScreenshot(TestContext.CurrentContext.Test.Name, "Failed");
    }
}

【讨论】:

  • 是的,这可行,但它不是很干燥,它需要我记住在每次测试结束时放置 TestJS。实际上,这是在一个基类中,有很多测试挂在它上面。我宁愿在拆解时这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多