Follow @RoyOsherove on Twitter

Creating custom test attributes easily with NUnit 2.2.1

One of the new features in NUnit 2.2.1 is the ability to extend the NUnit framework in two ways.
The first one is to be able to create your own custom test suites (for example, if you wanted to run your unit tests in a multi threaded fashion this would be a good candidate to go with).
The second one is the ability to create new test case attributes that add behavior to the running test case. A good example of that would be to be able to create a transaction in the beginning of a test and roll back that transaction at the end of it, thus rolling back any state that had changed in the database during the test.
 
I’ll show here how to do the second one, in a very easy manner.
Currently to go in the custom attribute route you need to create three separate classes to do your bidding.
  • A custom attribute class, which is just a fancy name for your intentions but does not actually implement anything
  • a custom test case, which overrides the standard Nunit test case behavior and lets you inject your on implementation along the way (this is where the real work is done)
  • a test case builder class which simply returns an instance of your new test case.
I won’t go into too much detail as to how you actually do this, because there’s a much much simpler way. In this alternate way to still get all the great benefits of the old way, but:
  • You create only one simple attribute class that inherits from a base class
  • You override two methods : “BeforeRunTest” and “AfterRunTest”
That’s it.
Here’s how it looks in this alternate way, if I were to create a class that simply writes to the console before and after the test is executed:
 

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]

[TestBuilder(typeof(CustomTestAttributeBase.CustomTestBuilder))]

public class SimpleTraceAttribute:CustomTestAttributeBase

{

   public override void BeforeTestRun (TestCaseResult testResult,

                                       TemplateTestCase testCase)

            {

                  Console.WriteLine(“START: “+ testCase.Name);

            }

 

   public override void AfterTestRun (TestCaseResult testResult,

                                    TemplateTestCase testCase)

            {

                  Console.WriteLine(“END: “+ testCase.Name);

            }

      }

 
 
There you go. No interception or custom version of NUnit is needed. Only the latest version which is NUnit 2.21 is needed for this.  Here are the steps to allow this cool thing in your project.
 
  1. Create a new class library project
  2. Add a reference to NUnit.Framework and Nunit.Core
  3. Download this base class and add it to your test project
  4. Start adding your own derived classes like shown above.
  5. That’s it.
The base class in the link actually implements all the three steps needed to create a custom test attribute, but allows you, the developer, to focus on the important things in life. you know, like, actually getting the job done and write a good unit test easily.
Have fun, and feel free to give feedback!

Extending NUnit *easily* with version 2.2.1 (finally!)

The Self Project - YAPP (Yet Another Programming Paradigm?)