views:

89

answers:

2

Hi,

I am trying to find out in Visual Studio 2010 Unit Testing how to keep a transaction of the data I have either added, updated, or deleted during my tests so on my TestCleanup I can rollback their values.

What search terms should I be using to find more about this?

Cheers

Paul

+2  A: 

Why do you need to rollback changes? Are your unit tests updating live data? If unit tests are written properly, you shouldn't have to clean up after what your tests changed because the data they're changing should be isolated to your test.

Edit:

It sounds like you've set up a data set for testing and want to make sure that data set is restored back to its original state. I prefer the practice of setting up the test data as part of the test, but I understand that can get difficult for complex tests.

If this in an ADO.NET data source, you could start a transaction then roll back that transaction at the end of your test. For example:

using (var transaction = db.BeginTransaction())
{
    // Do tests here
}
// The transaction is rolled back when disposed

Edit 2:

A third option, if you don't have transaction support, is to have a backup of your test data in a place where it won't get modified, then at the end of the test, restore that backup.

Jacob
No I am only using a test dataset. If my test performs a DELETE from the test database then really I want to rollback the test dataset after it has sucessfully performed the DELETE to it's original state so that I can use this data in another test.
Dr. Paul Jarvis
I see. Another approach you could take is to create the test data needed for the test as the setup for that test.
Jacob
+1  A: 

For unit testing you should probably try using mocks instead of accessing a test database. Unit tests should generally be completly self-contained (and not rely on external sources e.g., databases).

If you are actually testing calls to the database then this is probably an integration test in which case you could:

  1. Create test data in setup
  2. Run code
  3. Assert test passes
  4. Remove test data inserted in step one
Scozzard
I agree somewhat that unit testing should normally be self-contained and not rely on external sources.... but I am writing tests for my NHibernate Data Provider so I really need to be testing if this is working with the database.
Dr. Paul Jarvis
I'd drop step 4 and ensure step one can cope with data it does not expect.
mlk