Transactions in Integration Tests

I was working with some integration tests recently and had an epiphany: Why don't I use transactions in a test to reset data?  I'm referring to a case where an integration test does something like write a new row to a database table.  If I'm running these tests over and over those rows add up unnecessarily.  Wrapping the integration test with something like TransactionScope in .NET makes resetting this data real easy.

Take this hypothetical test in to consideration:

[Test]
public void AddDatabaseRow()
{
    DataContext data = new DataContext();

    SomeItem item = data.SaveItem(1, "abcd");

    Assert.IsNotNull(item, "Return data was null");
    Assert.AreEqual(1, item.UserID, "UserID did not match");
}

If I ran this test over and over again I'd have a duplicate table record added over and over again.  Sometimes that's not even possible when unique constraints are enforced such as having to have a unique email address.  The argument can be made that DataContext should be a mock object and not a live connection I suppose.  For argument's sake let's say there's a valid reason for doing an integration test this way:). 

Now let's look at a test using a transaction to reset the data when we're done.

[Test]
public void AddDatabaseRow()
{
    DataContext data = new DataContext();

    using (TransactionScope trans = new TransactionScope())
    {

        SomeItem item = data.SaveItem(1, "abcd");

        Assert.IsNotNull(item, "Return data was null");
        Assert.AreEqual(1, item.UserID, "UserID did not match");

    }
}

Using TransactionScope here performs a rollback after the assertions have been made and avoids permanently saving the database record to the table.

What do you think about this?  Is there a better way to go about achieving the same goal of not committing records often?


Bookmark and Share

Related posts

Comments

August 24. 2009 03:05 AM

Dentist Belfast

Your post is great thanks for sharing it i appreciate it i learn something new today.

Dentist Belfast

August 24. 2009 11:06 PM

Dental Implants Preston

I wanted to thank you for this great read!! I definitely enjoying every little bit of it

Dental Implants Preston

August 26. 2009 12:28 AM

Massage Golders Green

Great post, one of the most informative posts on this subject online actually.

Massage Golders Green

August 29. 2009 09:18 PM

Stop Dreaming Start Action

i feel very lucky can find this page..
finally i find something that i want to know..
thank you for this usefull informations..

Stop Dreaming Start Action

September 3. 2009 02:47 AM

Dental Implant Birmingham

Nice way of explaining things easily and with simpler concepts.

Dental Implant Birmingham

September 3. 2009 11:16 PM

Cosmetic Surgeon London

Oh my god..!! its really awesome, i loved it a lot, and the features are also good.

Cosmetic Surgeon London

September 11. 2009 10:55 PM

Dental Implant Cheshire

According to your information that is very simple and clearly understand by the people.

Dental Implant Cheshire

September 13. 2009 06:40 AM

payday online

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on. You have done a marvellous job!

payday online

September 13. 2009 07:47 AM

payday loans

Your blog provided us valuable information to work on. You have done a marvellous job!

payday loans

September 14. 2009 11:37 AM

Angioplasty

this is a good way to run integration tests without commiting records

Angioplasty

September 28. 2009 01:23 AM

US Gold Coin Auctions

Sound interesting for the integration test. I will give it a try and update it here.

US Gold Coin Auctions

October 10. 2009 07:12 PM

Payday Loan

Nice ... maybe you could update this. Thanks

Payday Loan

October 15. 2009 06:42 AM

no teletrack payday loans

Interesting information. May I add this blog to my linkexchange directory ?

no teletrack payday loans

October 25. 2009 04:35 PM

pay day loans

Like your writing! Still you can do some things to improve it.

pay day loans

November 5. 2009 08:52 PM

free online dating services

That is so great. Now I have found the right tool for me. You really gave me an idea on how to setup those things. Such a great help, thanks for the post and keep it up!

free online dating services

November 9. 2009 10:08 AM

Weight Loss Pills

Thanks for the update

Weight Loss Pills

Comments are closed