Dr. Paul Jarvis
Dr. Paul Jarvis

Reputation: 256

Visual Studio 2010 Unit Testing

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

Upvotes: 1

Views: 2218

Answers (2)

Jacob
Jacob

Reputation: 78900

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.

Upvotes: 3

Scott
Scott

Reputation: 1228

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

Upvotes: 1

Related Questions