This page last changed on Jul 02, 2005 by peli.

MbUnit supports setting dependencies between fixtures, similarly to NAnt/MsBuild task dependencies: if a test in a parent fixture fails, the child fixture are simply not executed.

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:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[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:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[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.

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[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...

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[TestFixture]
 public class Parent
 {
     [Test]
     public void Success()
     {}
 }

 [TestFixture]
 public class SickParent
 {
     [Test]
     public void Failure()
     {
         throw new Exception("boom");
     }
 }  

 [TestFixture]
 [DependsOn(typeof(Parent))]
 [DependsOn(typeof(SickParent))]
 public class Child
 {
     [Test]
     public void Success()
     {}
 }  

 [TestFixture]
 [DependsOn(typeof(Child))]
 public class GrandChild
 {
     [Test]
     public void Success()
     {}
 }

The output of the execution is given below. One clearly sees that Child.Success and GrandChild.Success were skipped
because SickParent.Failure failed:

 Info: Found 4 tests
 Info: [assembly-setup] success
 Info: [failure] SickParent.Failure
 TestCase 'SickParent.Failure' failed: boom
 ...
 Info: [success] Parent.Success
 Info: [skipped] Child.Success
 Info: [skipped] GrandChild.Success
 Info: [assembly-teardown] success
 Info: [reports] generating HTML report 

 1 succeeded, 1 failed, 2 skipped, took 0.00 seconds.
Document generated by Confluence on Jun 11, 2007 11:56