---
title: "Introducing: NUnitX and the Rollback attribute - Seamless database rollback with Nunit"
url: "https://osherove.com/blog/2004/7/11/introducing-nunitx-and-the-rollback-attribute-seamless-datab.md"
canonical: "https://osherove.com/blog/2004/7/11/introducing-nunitx-and-the-rollback-attribute-seamless-datab.html"
published: "2004-07-11T18:32:00"
updated: "2004-07-11T18:32:00"
tags: [".Net Original", "Agile Related"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

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

By Roy Osherove July 11, 2004 In [.Net Original](https://osherove.com/blog/category/net-original), [Agile Related](https://osherove.com/blog/category/agile-related)

**Historical article** · Originally published July 11, 2004; technical details may be outdated.

_**Update**: You may be more interested in [XTUnit](https://weblogs.asp.net/rosherove/archive/2004/10/05/238201.aspx), 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](https://osherove.com/rosherove/articles/dbunittesting.aspx) 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)](http://dev.magen.com/Agile/Code/NunitX.zip)

[Heres a link to XtUnit](http://teamagile.com/Downloads/XtUnit.zip) (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

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jul 19, 2004

    ### [Unit Testing scalability and performance](https://osherove.com/blog/2004/7/19/unit-testing-scalability-and-performance.html)

- Jul 18, 2004

    ### [Announcing:Coolest Add-in Contest - Entries available for download](https://osherove.com/blog/2004/7/18/announcingcoolest-add-in-contest-entries-available-for-downl.html)

- Jun 20, 2004

    ### [Watch me speak: eXtreme Programming and Agile concepts, at the .Net Architects user group this month](https://osherove.com/blog/2004/6/20/watch-me-speak-extreme-programming-and-agile-concepts-at-the.html)

- Jun 19, 2004

    ### [\[Article\] Simplified Database Unit testing using Enterprise Services](https://osherove.com/blog/2004/6/19/article-simplified-database-unit-testing-using-enterprise-se.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
