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.

Published Tuesday, October 05, 2004 3:26 PM by RoyOsherove

Comments

Tuesday, October 05, 2004 11:34 AM by TrackBack

# NUnit Extensibility Framework, OSS Lecture, etc...

Tuesday, October 05, 2004 11:13 PM by Ido Tandy

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

I've just tried it on a REAL project and it works like a charm...
I have Rollback without the need for NunitX...
as always, Roy, you're the king...
right mate ?!
Wednesday, October 06, 2004 12:32 AM by Sean McCormack

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

Awesome stuff...Thanks!
Wednesday, October 06, 2004 9:16 AM by Jonathan de Halleux

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

Hy Roy,

This looks very nice. I'll try it as soon as I get my box back. Would you be interrested in shipping this framework along with TestDriven.NET ? You framework would definitely have his place there :)
Wednesday, October 06, 2004 11:41 AM by Dave Montgomery

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

I echo Jonathan's sentiment. TestDriven.NET all the way.
Wednesday, October 06, 2004 8:03 PM by TrackBack

# And now RoyO is in on the act too...

Wednesday, October 06, 2004 8:04 PM by Mike Gale

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

The distance Unit testing has come inside .NET blows my mind. Those first JUnit ports are especially unimpressive when viewed from the vantage point of today.

Great work Roy and Peli.
Wednesday, October 06, 2004 8:21 PM by Trey Hutcheson

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

Roy,
Way to go dude. Very exciting stuff. I haven't downloaded it yet, but it's definitely on my to-do list. I started to have similar ideas a few days ago after reading the same article on gotdotnet.

Looking forward to playing with it.
trey
Wednesday, October 06, 2004 9:11 PM by Roy Osherove

# re: Introducing: Unofficial NUnit Extensibility Framework - make your own test attributes easily!

Peli (Jonathan): Great idea! This should be a part of TD.Net.
Monday, October 11, 2004 2:37 AM by Assaf K

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

Great idea and implementation.
Also I need your opinion: if I want to add utilities classes to the NUnit freamwork
Like you wrote before (DataComparison etc… remember?) Where shell I add this class? I guess I need to add it to the XtUnit.Extensions.XXX project am I right?
Thanks?
Monday, October 11, 2004 4:17 PM by TrackBack

# Introducing the Osherove.Interception framework

Monday, October 11, 2004 9:05 PM by TrackBack

# Introducing the Osherove.Interception framework

Monday, October 11, 2004 10:25 PM by MarkFocas

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

It appears that this only runs under Win 2003
Tuesday, October 26, 2004 6:03 PM by adult-galleries

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

Thursday, December 09, 2004 1:04 PM by TrackBack

# I changed my mind - Mock objects are wrong for database unit testing

Thursday, December 09, 2004 8:41 PM by TrackBack

# I changed my mind - Mock objects are wrong for database unit testing

Monday, December 20, 2004 4:17 AM by TrackBack

# Summary of Database Unit testing techniques

Saturday, January 22, 2005 11:31 AM by TrackBack

# Database Unit Testing issues and solutions: Rollback And RestoreDatabase abilities

Sunday, January 23, 2005 2:29 AM by TrackBack

# New stuff on Team Agile: XtUnit and Interception block with basic documentation

Wednesday, February 09, 2005 9:36 PM by TrackBack

# FreeDevelopmentTools

Wednesday, February 23, 2005 3:58 PM by TrackBack

# NUint的使用二 [原创]

Ping Back来自:blog.csdn.net
Friday, June 10, 2005 10:37 PM by TrackBack

# re: Rollback Attribute for NUnit 2.2.1 and Above

Saturday, May 27, 2006 6:51 AM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of colleagues. ...
Wednesday, May 31, 2006 2:05 AM by ISerializable - Roy Osherove's Blog

# Introducing: NUnitX and the Rollback attribute - Seamless database rollback with Nunit

Update: You may be more interested in XTUnit, which was released a bit later on and provides the same...
Wednesday, June 21, 2006 4:14 PM by ISerializable - Roy Osherove's Blog

# New stuff on Team Agile: XtUnit and Interception block with basic documentation

Update: Fixed broken links (21-6-06)
I just finished adding a new downloads section to the Team Agile...
Monday, July 24, 2006 12:52 PM by Colin Jack

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

I've tried using the using the DataRollbackAttribute but keep getting wierd MSDTC Transaction Errors: http://groups.google.co.uk/group/microsoft.public.dotnet.framework.component_services/browse_thread/thread/ae13b44beda812e5/29b5c9dfc68003fb?lnk=st&q=&rnum=1&hl=en#29b5c9dfc68003fb So far I've found it hard to pin point why I'm getting these problems, though most of the code I'm calling within the unit test uses its own transactions so that must be a factor. Anyway I wondered whether anyone else has met/conquered this problem?
Monday, November 27, 2006 12:59 PM by Peter Fitzgibbons

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

HI Roy,

I'm getting this on my testing solution when I try to use the SampleTestFixture.cs copied into my solution.  This is using TestDriven.net 2.0.1948 Personal :

TestCase 'M:dbTesting.SampleTestFixture.MyDataRelatedTest'

failed: Couldn't find declaring type with name 'dbTesting.SampleTestFixture'

Do you have an idea what's causing this ?

Thanks for your help!

Tuesday, December 19, 2006 9:54 AM by Cosimo Carbonelli

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

Hi, I'm using DataRollBack attribute and it works fine with Sql Server...compliments...but now I want to use it even with an application that uses an Oracle 9i db accessed by ODP.NET . Reading your article, I've thought that it must work anyway, because ODP.NET is based upon ADO.NET . Enterprise Services manages transactions at this level...but it doesn't work, or rather, the transaction doesn't roll back...do you have any idea?

Thursday, December 21, 2006 4:03 AM by Cosimo Carbonelli

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

Hi, I've forgot to say that the Oracle server is on a remote machine. This is probably the cause of the problem. Of course XtUnit works on the local DTC...it is possible to connect on the ServiceDomain object of a remote machine? And how to do that?

Thanks for your help

Cosimo

Tuesday, February 20, 2007 1:22 PM by Michael Larkin

# Used TransactionScope instead

I modified the code to use System.Transactions.TransactionScope...thus removing the need for the MSDTC and Enterprise Services.

public class RollBackAttribute:TestProcessingAttributeBase

{

       TransactionScope transactionScope;

[DebuggerStepThrough]

protected override void OnPreProcess()

{

try

{

               transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0,0,0,10000,0));

}

catch(Exception e)

{

OutputDebugMessage("Could not enter into a new transaction:\n" + e.ToString());    

}

}

       [DebuggerStepThrough]

protected override void OnPostProcess()

{

try

{

               Transaction.Current.Rollback();

               transactionScope.Dispose();              

}

catch(Exception e)

{

OutputDebugMessage("Could not leave an existing transaction:\n" + e.ToString());    

}

}

}