Geeks With Blogs
Matt Roberts Rails and .NET

Ok - no credit to me for any of this good stuff. All I'm doing here is posting on how to get database rollback working in your Nunit tests.

Roy Osherove created something called XtUnit - "An Unofficial Unit Testing Extensibility Framework - Add new attributes to NUnit or MbUnit easily" -

His blog talks you through all the cool stuff that you can do with this framework, like implementing your own attributes to do cool things pre and post method execution. But the thing I wanted more than anything was to add rollback powers to existing NUNit tests, I didn't want to switch to MbUnit to do this, I just wanted to be able to do it with my existing tests.

It was pretty simple to get working, but the documentation is a tiny bit in-complete, so this is what you need to do.

I'm in Visual Studio 2008, using LLBLGenPro to talk to the database (obviously you don't need to use LLBLGen Pro, but why the hell not!)

  1. Download the binaries. You could download the source but you'll need to migrate it to 2008, and I didn't need the src.
  2. Reference the binary file that is downloaded in your test project.
  3. Make sure that your test fixture class derives from ExtensibleFixture
  4. Add the DataRollBack attribute to your test method


Here's the code for completion. My example as basically adding a row to a table called "GlobalSearch", in case the LLBLGen stuff looks confusing to you.

   1: [TestFixture]
   2: public class Class1 : ExtensibleFixture
   3: {
   4:     [Test, DataRollBack]
   5:     public void TestDataRollBack()
   6:     {
   7:         var adapter = new DataAccessAdapter("connection string here", false, CatalogNameUsage.ForceName, "");
   8:         // Create a new GlobalSearchEntity, save it, then rollback
   9:         var g = new GlobalSearchQueueEntity();
  10:         g.RecordId = 100;
  11:         g.ToolCode = "TOOLCODE";
  12:         adapter.SaveEntity(g);
  13:     }
  14: }
Posted on Thursday, December 18, 2008 3:26 PM | Back to top

Comments on this post: Get Database rollback powers in your NUnit Tests!

# re: Get Database rollback powers in your NUnit Tests!
Requesting Gravatar...
This is brilliant. Question, though, is there a way to do something similar with MSTest? Since switching over to VS2008 I decided to use MSTest instead of NUnit...
Left by SanjayU on Dec 21, 2008 10:07 PM

# re: Get Database rollback powers in your NUnit Tests!
Requesting Gravatar...
Not sure about MSTest, but I don't know of anything....

My advice - use Nunit instead :)
Left by Matthew Roberts on Mar 21, 2009 10:38 AM

Your comment:
 (will show your gravatar)

Copyright © Matt Roberts | Powered by: | Join free