NUnit Test Actions for Great Good

Lots of c# unit tests I see build up a set of abstract base classes to allow for things like setting up common environments and handling common startup and teardown tasks. This is never a good approach, in my opinion, and can lead to some difficult workarounds due to the high level of coupling this implies. NUnit has a better approach by allowing a text fixture or method to be decorated with a custom attribute that allows code to be run before and/or after all the tests in that test fixture or just the one affected test method.


using NUnit.Framework;

[TestFixture]
[CommonTestHousekeeping]
public class ExampleTest
{
	[Test]
	public void Example_Must_Do_Housekeeping_First()
	{
		... 
	}
}


using NUnit.Framework;

... 

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
public class CommonTestHousekeepingAttribute : Attribute, ITestAction
{
	public ActionTargets Targets
	{
		get { return ActionTargets.Default; }
	}

	public void BeforeTest(TestDetails testDetails)
	{
        // do stuff here 
		// ...
		
		if (NotAbleToDoTheThingIWasSupposedToDo)
		{
			// report an error back to the NUnit framework
			throw new ApplicationException("Could not do the thing");
		}
	}

	public void AfterTest(TestDetails testDetails)
	{
        // do stuff here 
		// ...
	}
}


Now rather than having all that coupling to a base class, if you decide your test no longer needs the housekeeping, you remove the attribute and nothing else changes.