Matt Roberts

Rails and .NET

  Home  |   Contact  |   Syndication    |   Login
  37 Posts | 0 Stories | 34 Comments | 35 Trackbacks

News




Twitter












Archives

Post Categories

BlogRoll

Entropy

Thursday, December 18, 2008 #

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" - http://weblogs.asp.net/rosherove/archive/2004/10/05/238201.aspx

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

Done!

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: }