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 NUnit.Extensions.ExtensibilityFramework 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:
- 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)
- Nunit.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.
- 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 NUnit.tExtensionsFramework project 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.