Roy Osherove

View Original

Introducing: XtUnit . An Unofficial Unit Testing Extensibility Framework - Add new attributes to NUnit or MbUnit easily!

Update: I've uploaded a more complete version of the framework and also changes the file name. please get it from here:

http://www.teamagile.com/downloads/TeamAgile.XtUnit.Bin.zip

http://www.teamagile.com/downloads/TeamAgile.XtUnit.src.zip

if you have the earlier version.  (and just so you know - it works perfectly with the TestDriven.Net tool suite)


 

One of the things that have bothered me the most since I got into the whole "Add database rollback to your unit tests" thing, is how much work it takes to make your test suite use this feature. I actually went and made a new binary of the NUnit Framework to support this new attribute. All this because there is no clear extensibility model for NUnit these days. Peli, on the other hand has a very nice way of extending MbUnit, but it still entails recompiling his library for this to work (or am I wrong?)

so - an idea came to me. A while ago Peli told me he had just found out about ContextBoundObjects and the ability to "intercept" method calls for pre and post processing. He said it might have some cool things that can be used with unit testing but we couldn't find something that was really cool to do with it.

The other day, while reading this nice article about implementing interception in your code, I got an idea: Maybe interception and Contexts are the best way for extensibility? So, I gave it a shot. And it turns out pretty darn cool I have to say.

Introducing the XtUnit.Framework project

With this project you are now able to add any attribute you can think of to you NUnit (or any other xUnit tests) with the ease of simply deriving from a base class. In the solution that you can download you will find 3 projects:

  • XtUnit.Framework: The Extensibility framework base classes (2 of them - one to derive your test fixtures from, and one if you want to create your own attributes)
  • XtUnit.Extensions.Royo is a project that I made that uses these bases classes to add two attributes. The the now known [Rollback] attribute, and the second one is a simple [Tracing] Attribute. You can see the code and realize how simple it really is.
  • XtUnit.SampleTests: a sample project with one simple fixture that uses the custom attributes.

now here are the cool things:

  • You can now *fully* debug and step into your tests, and the [Rollback] attribute works perfectly!
  •  You are not depending on any specific framework! Simply compile the XtUnit.Framework and XtUnit.Extensions.Royo projects with your Testing framework of choice and that's that! no more NunitX variants, thank god! Yes. This means you can use your existing NUnit 2.x projects with these attributes and even add your won. Just add these two projects to your solution and you're done.
  • It is very easy to create your own attributes (and you can see how easy in the XtUnit.Extensions.Royo project)

 

I'd love to get your feedback. Have fun :)

I'd like to thank Jamie for helping me solve two simple and annoying bugs I just couldn't find with my thick head.