tags:

views:

59

answers:

3

I've got a bunch of tests in NUnit which create garbage data on the filesystem (bad, I know, but I have little control over this). Currently we have a cleanup tool that removes these temporaries and such, but I'd like to be able to run that cleanup tool automatically. I'd have to be able to run it after all tests have finished running. I have similar checking that I'd like to do at the beginning, to ensure that there are none of these temporaries left from previous runs that might change the outcome of the tests.

Is such a thing simple or am I going to have to implement a whole new test runner for such a thing?

+5  A: 

Yes,

Use the [SetUpFixture] attribute on a class and the [SetUp] and [TearDown] attributes on methods with that class.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution. In the examples below, the method RunBeforeAnyTests() is called before any tests or setup methods in the NUnit.Tests namespace. The method RunAfterAnyTests() is called after all the tests in the namespace as well as their individual or fixture teardowns have completed exection.

Source (it says 2.4 on the page, but it is available in 2.5)

ChrisF
That'd be perfect (Exactly what I was looking for in fact) except it's NUnit 2.4 only. :(
Billy ONeal
ChrisF
Yes.. I'm using 2.5 though....
Billy ONeal
@Billy - the attributes are still available in 2.5
ChrisF
@ChrisF: Ah. +1 then.
Billy ONeal
+4  A: 

Take a look at TestFixtureSetUp and TestFixtureTearDown.

ThatBlairGuy
Hmm.. that requires that I have every test be part of the same fixture. I'd rather not do that if at all possible. But +1.
Billy ONeal
Well, you could always have the Setup/Teardown routines call something shared. Or for that matter, use a common base class for all of your fixtures.
ThatBlairGuy
A: 

Yes, use SetupFixture attribute on a class, and use [SetUp] for initializing before tests are run and [TearDown] for cleaning up afterwards.

driis
Is there a way to do it *globally* though?
Billy ONeal
@Billy: Have all your test-classes derive from a common class.
BlueRaja - Danny Pflughoeft
@BlueRaja - Danny Pflughoeft: I'd really rather avoid manually going through thousands of test cases and manually changing all of them to inherit from a class.
Billy ONeal