views:

180

answers:

1

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
+1  A: 

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

AdaTheDev
Good suggestion, but I have to run the cleanup after specific tests to be ready for the next test.Hadn't thought about a different test fixture. Thanks for the recommendation.
Mark Struzinski
No worries, there was a very similar question the other day: http://stackoverflow.com/questions/1194198/how-can-i-skip-the-setup-method-only-for-a-particuler-test-in-nunit/1194213#1194213Also, I've just thought of an alternative..please see edit in my answer. I don't think it's the best solution/or particularly something you'd want to spend time on, but will include anyway.
AdaTheDev
@Mark - Good practise is to never have any tests rely on each other
AutomatedTester
@AutomatedTester - that's why I was trying to incorporate this into a TearDown method - and also to stop repeating myself by writing cleanup code.
Mark Struzinski