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 abilities, without needing a seperate version of NUnit to run. In fact, it runs on any unit testing framework for .NET which can use attributes.

My article on performing database unit tests with rollback ability has managed to get quite a lot of attention. There were some caveats to that method, though:

  • You had to inherit from ServicedComponent thus limiting your inheritance chain
  • It meant you had to strong name your assemblies
  • Some trouble was encountered when running the tests through the NUnit GUI (disposing objects too soon) for some people.
  • All tests were run under the same transaction context

The other way to go on this was to use the method called "Services without components" - mainly utilising the ServiceDomain and the ServiceConfig classes found in the System.EnterpriseServices namespace, but are relatively unknown or unused. I won't extend the discussion of the actual technicalities here because I'm planning at article on this subject, but for now you can enjoy the fruits of this research today.

What I really wanted was to be able to write something as elegant as this:

[Test,Rollback]

public void Insert()

{

      //Do some inserts into the DB here

      //and the automatically roll back

}

So, I dug deep into the bowels of the Nunit Framework and came out with something that did exactly that -  I added a new attribute to the framework -

RollbackAttribute

by placing that attribute on a test method that test case will automatically run inside its own transaction context, and automatically roll back at the end of the test. It's really quite cool and I'm using it today in our applications.

yes. using it looks exactly like this:

[Test,Rollback]

public void Insert()

{

      //Do some inserts into the DB here

      //and the automatically roll back

}

So - I went ahead and compiled a custom version of the NUnit framework just for you. I like to call it NunitX - it is a mutant and as such must have a cool name. Hope you agree. To use - simply add a reference to the NUnit.framework.dll found in the zip file instead of the one you usually reference. that's basically it.

Here's a direct download link for the zip file (130k)

Heres a link to XtUnit (as mentioned in the beginning of this post). You may find this a better a implementation that does not need its own version of NUnit to work.

USAGE NOTES
-------------
* windows XP SP 1 or above required (COM+ 1.5 is required for this to work - that's why)
* this version is using Services without components = no strong naming assemblies is needed
* the NUnit Add-in will work, but when you use the Nunit-Addin you will *not* have Rollback abilities
* Each Test method will run in its own separate transaction
* If you have an object down the line which uses its own transaction and calls Commit on it - you will not be able to rollback that commit.
 This will only happen if that object uses the TransactionOption.RequiresNew flag.
 for objects that use the TransactionOption.Required flag - rollback will work just fine.
* you need to use the Nunit-gui and nunit-console versions that come with this distribution.
 you CANNOT use the original NUnit gui or console applications to run tests that use this custom framework

Published Monday, July 12, 2004 12:32 AM by RoyOsherove

Comments

Sunday, July 11, 2004 11:09 AM by TrackBack

# Automatic RollBack of TestCase in MbUnit

Sunday, July 11, 2004 11:20 AM by TrackBack

# MBUnit to feature Rollback abilities as well

Sunday, July 11, 2004 2:16 PM by TrackBack

# Re: NUnitX and the Rollback attribute

Re: NUnitX and the Rollback attribute
Sunday, July 11, 2004 7:05 PM by TrackBack

# NUnitX and RollbackAttribute

Monday, July 12, 2004 12:49 AM by Chris Garty

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

Hi Roy,

Excellent to see that you put it into NUnit. You should post the code to the NUnit developers list so it can be included in future versions of NUnit.

Great work!

- Chris
Monday, July 12, 2004 5:45 AM by TrackBack

# NUnitX DB testing/rollback from Roy Osherove

Monday, July 12, 2004 5:46 AM by TrackBack

# NUnitX DB testing/rollback from Roy Osherove

Monday, July 12, 2004 8:21 AM by Jerrad Anderson

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

yep... great work indeed :)
Monday, July 12, 2004 1:06 PM by TrackBack

# NUnitX: NUnit con RollBack o Haz pruebas sin romper la BBDD

Monday, July 12, 2004 2:39 PM by TrackBack

# NUnit - Database Testing and Automatic Rollback - Unit Testing

Tuesday, July 13, 2004 1:40 AM by TrackBack

# NUnit - Database Testing and Automatic Rollback - Unit Testing

Tuesday, July 13, 2004 3:28 AM by TrackBack

# Automatic RollBack of TestCase in MbUnit

Tuesday, July 13, 2004 3:28 AM by TrackBack

# Automatic RollBack of TestCase in MbUnit

Tuesday, July 13, 2004 5:59 AM by Eric Vautier

# NUnitX introduces Seamless database rollback

Announcement on testdriven.com
Monday, August 09, 2004 11:38 PM by Sam

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

This is really cool, but I got a FileNotFoundException. It says that NUnit.Core.dll or one of its dependencies was not found? Any thoughts? I'm running xp. Thanks, Sam
Monday, August 23, 2004 1:49 PM by TrackBack

# Stuff to do

Monday, August 23, 2004 10:29 PM by TrackBack

# TDD rant and raves

TDD rant and raves
Friday, June 10, 2005 6:58 PM by TrackBack

# Rollback Attribute for NUnit 2.2.1 and Above

Saturday, June 11, 2005 6:16 PM by TrackBack

# Rollback Attribute for NUnit 2.2.1 and Above

Tuesday, May 30, 2006 9:46 PM by Chloe

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

Hi, I can't download it at all. It shows Connection close. Can you fix it? I really wanna use it for rollback purpose.
Wednesday, May 31, 2006 2:06 AM by RoyOsherove

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

I've fixed the link, and added a new link to XTUnit, which is a better implementation of this aproach, that does not need its own version of NUnit to work.

Roy.
Monday, July 24, 2006 3:41 PM by Colin Jack

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

Has anyone managed to get this working in VSTS, I keep getting wierd MSDTC errors.
Tuesday, October 10, 2006 4:19 AM by the stranger

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

I tried your framework (rollback function) with Team System Unit Testing and Nunit Framework (Resharper gui integrated in VS.net) and it doesn't work with the error : System.Data.OleDb.OleDbException: Aucune message d'erreur disponible, code de résultat : E_NOINTERFACE(0x80004002). Server stack trace: à RssReader.DataAccessLayer.FeedsImpl.AddFeed(Feed currentFeed) dans C:\dev\test\RssReader\RssReader.DataAccessLayer\FeedsImpl.cs:ligne 139 à RssReader.TestProject.FeedsImplTest.AddFeedTest() dans C:\dev\test\RssReader\RssReader.TestProject\FeedsImplTest.cs:ligne 103 à System.Runtime.Remoting.Messaging.Message.Dispatch(Object target, Boolean fExecuteInContext) à System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) Exception rethrown at [0]: à System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) à System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) à RssReader.TestProject.FeedsImplTest.AddFeedTest() For information, i try to insert a simple line in an Access database. Thx in advance for your help.
Friday, December 08, 2006 6:57 PM by Chuck Sphar

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

Hi Roy--

Still unable to download NUnitX from this page, or to link to XTUnit.

thanks,

Chuck

# Eli Lopian’s Blog (TypeMock) » Blog Archive » Rollback for database testing

Pingback from  Eli Lopian’s Blog (TypeMock)  » Blog Archive   » Rollback for database testing