views:

21

answers:

1

Hello guys,

The questions is pretty straightforward and I need to know how to create multiple cleanup tests.

I have some tests, and each test creates a different file. I would like to bind a cleanup action to each, so I can delete the specified files for each test.

eg:

[TestMethod]
public void TestMethodA()
{
// do stuff
}

[TestMethod]
public void TestMethodB()
{
// do stuff
}

[TestCleanup]
public void CleanUpA()
{
// clean A
}

[TestCleanup]
public void CleanUpB()
{
// clean B
}

Any ideas?

A: 

There are, potentially, a couple of options that I can see. A simple solution that might work for you is to have a class-level variable in your unit test class that stores the path to the file used by the currently executing test. Have each test assign the current file path to that variable. Then you can have a single cleanup method that uses that variable in order to clean up the file.

Another idea, but one that might require significant refactoring, is to use a dependency-injection approach in order to abstract your code from the file system. Perhaps instead of your code creating/opening the file itself, you could have a an IO abstraction component handle creating the file and then just return a Stream object to your main code. When running your unit tests you could provide your main code with a unit testing version of the IO abstraction component that will return a MemoryStream instead of a FileStream, which would avoid the need to perform cleanup. I could update with a rough example if my explanation isn't clear.

Dr. Wily's Apprentice