Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit Conditional Teardown?

Is there a way to do a conditional TearDown in NUnit?

I have a TestFixture which has a need to run cleanup code for just a few tests, and I don't really want to:

  1. Run the TearDown method on every test
  2. Create a private helper method and call it from the tests requiring cleanup if I can avoid it
like image 560
Mark Struzinski Avatar asked Nov 06 '22 20:11

Mark Struzinski


1 Answers

There isn't unfortunately.

Can you not do the cleanup in the [TestFixtureTearDown] instead, so once all the tests have finished? I guess that depends on whether the cleanup has to be done before the next test runs.

Alternatively, put those tests that require a cleanup in another class/TextFixture together, away from the other tests. Then you can use a TearDown in there which doesn't need to be conditional.

Edit: One thing I've just thought of, which could be done to achieve the aim though probably isn't actually worth it for this particular need, is that you can extend NUnit - create your own custom attributes which you could handle however you wanted. This is mentioned here. Like I say, I don't think really you should go down that route for this, but is useful to know none-the-less

like image 85
AdaTheDev Avatar answered Nov 09 '22 13:11

AdaTheDev