简体   繁体   English

在拆解中未通过NUnit测试

[英]Failing an NUnit test inside teardown

I have a bunch of automated UI tests running with selenium and NUnit. 我有一堆用selenium和NUnit运行的自动UI测试。

After every nunit test I want to check the browser for any JS errors that have occurred. 在每次nunit测试之后,我想检查浏览器是否有任何已发生的JS错误。 If there are any JS errors the test that caused them should fail. 如果存在任何JS错误,则导致它们的测试将失败。 I want this to run for all tests I write without having to copy the check into each test. 我希望这可以运行我编写的所有测试,而无需将检查复制到每个测试中。

I also take screenshots on any failure. 我也会截上任何失败的截图。

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

If I fail an assertion inside teardown it will never execute the screenshot code (as the assert is an exception). 如果我在拆解内部失败,它将永远不会执行屏幕截图代码(因为断言是一个例外)。

Is there a better way to fail the test here so that I can carry on with my logic? 有没有更好的方法来使测试失败,以便我可以继续我的逻辑?

You could extend your own TestActionAttribute that runs your JS tests. 您可以扩展自己运行JS测试的TestActionAttribute If an assertion failure is thrown in AfterTest() , NUnit seems to report the test as being failed. 如果在AfterTest()抛出断言失败,NUnit似乎报告测试失败。

Eg this: 例如:

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

Produces this failure in NUnit: 在NUnit中产生此故障:

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

I've declared the [CheckForJSErrors] attribute for the entire test fixture to avoid having to declare it on every test case, but you could declare it per test method if you want. 我已经为整个测试夹具声明了[CheckForJSErrors]属性,以避免在每个测试用例中声明它,但如果需要,可以按照测试方法声明它。

If you want to check the same thing after every test, you could just right a common method that you call in each test itself. 如果你想在每次测试后检查相同的东西,你可以恰到好处地在每个测试中调用一个常用的方法。 That way you're not duplicating the test code and you are keeping the fail assert out of the tear down. 这样你就不会复制测试代码了,而且你正在将失败断言保持在拆除状态之外。

Eg. 例如。

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

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

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