Saturday, October 09, 2004

Another keeper from MbUnit: AssemblySetUp/TearDown

Now that I've installed the latest MbUnit from TestDriven.NET I can make use of the TestFixtureSetUp and TestFixtureTearDown support in MbUnit (+ Assembly Setup and TearDown).

This has greatly simplified my code. In each of my test fixtures I used to have to have a SetUp method which calls a static initializer from another class to initialize my log4net stuff:

public class MyTests

{
[SetUp]
public static void SetUp()
{
StaticInitializer.Initialize()
}
[Test]
public voidSomeTest()
{
...
}
}

public static class StaticInitializer
{
private static bool s_bIsInitialized = false;
public static void Initialize()
{
if (!s_bIsInitialized)
{
log4net.Config.BasicConfigurator.Configure (...);
s_bIsInitialized = true;
}
}
}
Now all I need is:
public class MyTests

{
//No extra stuff at all
[Test]
public voidSomeTest()
{
...
}
}

[assembly: AssemblyCleanup (typeof (StaticInitializer))]
public static class StaticInitializer
{
[SetUp]
public static void Initialize()
{
log4net.Config.BasicConfigurator.Configure (...);
}
}
The rest is handled for me.

Sooo much nicer.

1 comment:

Peli said...

And don't forget you can also cleanup things in the TearDown method, which is not acheivable using a static constructor approach :)

Peli.