MbUnit now supports setting dependencies between fixtures, similarly to NAnt/MSBuild task dependencies.
How-to
The logic is simple. You can tag a fixture with the DependsOnAttribute to specify a "parent" fixture that should be run before the current fixture. If the parent fixture fails, the current fixture is not run:
[TestFixture]
public class Parent
{...}
[TestFixture]
[DependsOn(typeof(Parent))]
public class Child
{...}
The Child fixture is executed if the Parent fixture executed successfully.
Why do we need dependencies between fixtures ?
Image that you are testing a database. At first, you create a fixture that tests that the connection is correct:
[TestFixture]
public class ConnectionTest
{ ... }
If one of the tests of ConnectionTest fails, then it is likely that all the tests that rely on a database connection will fail and, therefore, should not be executed. Using the DependsOnAttribute, you can specify the dependency of such fixture to the ConnectionFixture.
[TestFixture]
[DependsOn(typeof(Connection))]
public class DALTest
{...}
An example
The following example defines 4 fixture. Child depends on Parent and SickParent, GrandChild depends on Child. Since SickParent has a failed test, Child and GrandChild should not be executed...
[TestFixture]
[CurrentFixture]
public class Parent
{
[Test]
public void Success()
{}
}
[TestFixture]
[CurrentFixture]
public class SickParent
{
[Test]
public void Failure()
{
throw new Exception("boom");
}
}
[TestFixture]
[CurrentFixture]
[DependsOn(typeof(Parent))]
[DependsOn(typeof(SickParent))]
public class Child
{
[Test]
public void Success()
{
}
}
[TestFixture]
[CurrentFixture]
[DependsOn(typeof(Child))]
public class GrandChild
{
[Test]
public void Success()
{}
}
The report result is shown below and one can see that Child and GrandChild fixture have failed without being executed.
