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?
-
1Can you please change the accepted answer, because it's changed in NUnit 3.x? – Andrew Rondeau May 30 '19 at 21:49
6 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();
}
}

- 1
- 1

- 3,399
- 2
- 25
- 31
-
Thanks for the link. Please note that this question was posted over a year and a half before David's answer. – George Mauer May 06 '13 at 20:00
-
7In Nunit 3.x it is `if (TestContext.CurrentContext.Result.Outcome == ResultState.Failure)` – Lukas Jul 11 '16 at 15:44
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
}
}

- 667
- 7
- 18

- 196
- 1
- 3
-
I would recommend to test for status failed and error: `TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed || TestContext.CurrentContext.Result.Outcome == ResultState.Error` – Damian Vogel Jan 20 '22 at 18:53
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

- 20,469
- 14
- 82
- 108
-
1Thats upsetting, I would have expected some sort of NUnit.CurrentTestContext API. I just need to know if the test succeeded as there is additional info that I can provide in this fixture if it failed that would help in debugging. – George Mauer Sep 24 '09 at 19:14
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. 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. If the test throws an exception it won't set the SUCCEDED. you can also put this in a try catch finally block in your tests with a slightly different logic:
[Test]
void Test1()
{
test1State = TestStateEnum.SUCCEDED; // On the beginning of your test
try
{
... // Your Test
}
catch( Exception )
{
test1State = TestStateEnum.FAILED;
throw; // Rethrows the Exception
}
}

- 2,665
- 18
- 33
-
Its not an attempt to tell which test was run, just a different error that I would like to throw so i can get a more accurate stacktrace – George Mauer Sep 24 '09 at 19:11
-
The problem with this solution is that every test must be edited. It doesn't scale. See https://stackoverflow.com/a/40635969/1711103 for a generalized solution. – Andrew Rondeau May 30 '19 at 21:48
[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");
}
}

- 31
- 3
IMHO tear down logic should be independent of test results.
Ideally you should avoid using setup and teardown completely, a al xunit.net. See here for more info.

- 29,453
- 4
- 60
- 67
-
As a rule, absolutely that is what you should do. However I like to keep my tests clean and cohesive with a BDD flavor. Sometimes that means setting up givens and asserts via lambdas and executing them in the TearDown. The problem is if for some reason the setup or test body failed the stacktrace is confusing. – George Mauer Sep 24 '09 at 19:09