Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Blogger dinner with Johanna Rothman - Rescheduled | Main | Creating custom test attributes easily with NUnit 2.2.1 »
Sunday
Dec052004

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

I finally got some time so I sat down with the latest version of NUnit - 2.2.1 and tried using its extensibility features like the ability to create new custom test attributes that change test behavior, hopefully ridding myself of more complex projects that I had to do in the past (such as using interception or create a whole new binary version of Nunit just so that this could be done) .
I'm glad to say that it worked out great! NUnit 2.2.1 finally allows one to (no so)easily add custom attributes such as Rollback to NUnit without resorting to violence. Yey!
Unfortunately, the process is still too bureaucratic (for me at least), requiring the developer to add 3 new classes for each test attribute. I took it upon myself to check and see if this process could be made easier and it does :)
 
I basically created a base class you inherit from and does all the nasty work for you. Here's how easy it is to create a new attribute that does tracing on your unit tests:
 

[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);

            }

}

 
Simple and quick, no fuss.
 

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>