Tuesday, April 3, 2007

Fixture order with MbUnit

In my company we have some people who are using NUnit and some are using MbUnit. I’m personally a huge fan of MbUnit. As a company we made a decision to use MbUnit only. Someone at my company had a problem with MbUnit because it would run tests in random order. In his case he wanted to run TestFixtures in specific order. He took advantage that NUnit runs TestFixtures in alphabetical order and named the fixtures in order he wants to run them. For example:

A_Fixture
B_Fixture

N_Fixture

To help to solve his problem I reflected on MbUnit.Framework.dll, and found DependOnAttribute. And that exactly what he wanted.

Here how it works. Let assume that we created four.

A_Fixture
Bad_Fixture
B_Fixture
B_Child_Fixture

[TestFixture]
public class A_Fixture
{
[
Test]
public void Success()
{ }
}

[TestFixture]
public class Bad_Fixture
{
[
Test]
public void Failure()
{
Assert.IsTrue(false);
}

[Test]
public void Success()
{
}
}

[TestFixture]
[
DependsOn(typeof(A_Fixture))]
[
DependsOn(typeof(Bad_Fixture))]
public class B_Fixture
{
[
Test]
public void Success()
{
}
}

[TestFixture]
[
DependsOn(typeof(B_Fixture))]
public class B_Child_Fixture
{
[
Test]
public void Success()
{
}
}

You can see that B_Child_Fixture depends on B_Fixture. B_Fixture at the same time depends on A_Fixture and Bad_Fixture. As you can see one of the tests in Bad_Fixture will fail. As a result B_Fixture and B_Child will not run.

Here's the result in the MbUnit GUI.


3 comments:

Joseph said...

Thanks Vadim,

Joe

Anonymous said...

Archaic

Nachiket said...

I just want to add that MbUnit also takes a method name in DependsOn attribute. This way if you have methods in one testfixture that you want to execute in some order, you can achieve that.