简体   繁体   English

NUnit - 是否可以在TearDown中检查测试是否成功?

[英]NUnit - Is it possible to check in the TearDown whether the test succeeded?

I would like to have my TearDown method check whether the previous test was a success before it applies some logic. 我希望我的TearDown方法在应用某些逻辑之前检查先前的测试是否成功。 Is there an easy way to do this? 是否有捷径可寻?

This has been already solved in Ran's answer to similar SO question. Ran对类似SO问题的回答中已经解决了这个问题。 Quoting Ran: 引用冉:

Since version 2.5.7, NUnit allows Teardown to detect if last test failed. 从版本2.5.7开始,NUnit允许Teardown检测上次测试是否失败。 A new TestContext class allows tests to access information about themselves including the TestStauts. 新的TestContext类允许测试访问有关自身的信息,包括TestStauts。

For more details, please refer to http://nunit.org/?p=releaseNotes&r=2.5.7 有关更多详细信息,请参阅http://nunit.org/?p=releaseNotes&r=2.5.7

[TearDown]
public void TearDown()
{
    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        PerformCleanUpFromTest();
    }
}

If you want to use TearDown to detect status of last test with NUnit 3.5 it should be: 如果你想使用TearDown来检测NUnit 3.5的最后一次测试的状态,它应该是:

[TearDown]
 public void TearDown()
 {
   if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
   {
      //your code
   }
 }

sounds like a dangerous idea unless it's an integration test, with say data to remove say. 听起来像一个危险的想法,除非它是一个集成测试,说删除数据说。 Why not do it in the test itself? 为什么不在测试中做呢?

Obviously a private flag in the class could be set. 显然,可以设置班级中的私人旗帜。

This is what Charlie Poole himself has suggested if you must 如果你必须的话,这就是查理普尔自己所建议的

Only if you do this manually. 仅当您手动执行此操作时。 In fact you even won't know which tests are intend to run. 实际上你甚至不知道打算运行哪些测试。 In NUnit IDE one can enable some tests and disable some other. 在NUnit IDE中,可以启用一些测试并禁用其他测试。 If you want to know if some specific test has run you could include code like this in your test class: 如果您想知道某个特定测试是否已运行,您可以在测试类中包含这样的代码:

enum TestStateEnum { DISABLED, FAILED, SUCCEDED };
TestStateEnum test1State = TestStateEnum.DISABLED;

[Test]
void Test1()
{
test1State =  TestStateEnum.FAILED; // On the beginning of your test
...
test1State =  TestStateEnum.SUCCEDED; // On the End of your Test
}

Then you can check the test1State variable. 然后你可以检查test1State变量。 If the test throws an exception it won't set the SUCCEDED. 如果测试抛出异常,则不会设置SUCCEDED。 you can also put this in a try catch finally block in your tests with a slightly different logic: 您也可以在测试中使用稍微不同的逻辑将它放在try catch finally块中:

[Test]
void Test1()
{
test1State =  TestStateEnum.SUCCEDED; // On the beginning of your test
try
{
    ... // Your Test
}
catch( Exception )
{
   test1State =  TestStateEnum.FAILED;
   throw; // Rethrows the Exception
}
}
    [OneTimeTearDown]
    public void AfterEachTest()
    {
        if (TestContext.CurrentContext.Result.Outcome.Status.Equals(TestStatus.Failed))
        {
           Console.WriteLine("FAILS");

        }
        else if (TestContext.CurrentContext.Result.Outcome.Equals(ResultState.Success))
        {
            Console.WriteLine("SUCESS");

        }
    }

IMHO tear down logic should be independent of test results. 恕我直言拆除逻辑应独立于测试结果。

Ideally you should avoid using setup and teardown completely, a al xunit.net. 理想情况下,你应该完全避免使用设置和拆解,al xunit.net。 See here for more info. 有关详细信息,请参见此处

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

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