Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

nunit

I would like to have my TearDown method check whether the previous test was a success before it applies some logic. Is there an easy way to do this?

like image 320
George Mauer Avatar asked Sep 24 '09 17:09

George Mauer


People also ask

What is TearDown in NUnit?

This attribute is used inside a TestFixture to provide a common set of functions that are performed after each test method. TearDown methods may be either static or instance methods and you may define more than one of them in a fixture.

What is test runner in NUnit?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests. It allows you to selectively run single tests or suites and reloads automatically as you modify and re-compile your code.

What is test attribute in NUnit?

The Test attribute is one way of marking a method inside a TestFixture class as a test. It is normally used for simple (non-parameterized) tests but may also be applied to parameterized tests without causing any extra test cases to be generated.


2 Answers

This has been already solved in Ran's answer to similar SO question. Quoting Ran:

Since version 2.5.7, NUnit allows Teardown to detect if last test failed. A new TestContext class allows tests to access information about themselves including the TestStauts.

For more details, please refer to http://nunit.org/?p=releaseNotes&r=2.5.7

[TearDown] public void TearDown() {     if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)     {         PerformCleanUpFromTest();     } } 
like image 117
Robert Važan Avatar answered Sep 21 '22 06:09

Robert Važan


If you want to use TearDown to detect status of last test with NUnit 3.5 it should be:

[TearDown]  public void TearDown()  {    if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)    {       //your code    }  } 
like image 29
Karolina Majde Avatar answered Sep 22 '22 06:09

Karolina Majde