views:

323

answers:

6

When you are doing integration tests with either just your data access layer or the majority of the application stack. What is the best way prevent multiple tests from clashing with each other if they are run on the same database?

+7  A: 

For simple database applications I find using SQLite invaluable. It allows you to have a unique and standalone database for each test.

However it does only work if you're using simple generic SQL functionality or can easily hide the slight differences between SQLite and your production database system behind a class, but I've always found that to be fairly easy in the SQL applications I've developed.

Free Wildebeest
+5  A: 

Transactions.

What the ruby on rails unit test framework does is this:

Load all fixture data.

For each test:

  BEGIN TRANSACTION

    # Yield control to user code

  ROLLBACK TRANSACTION

End for each

This means that

  1. Any changes your test makes to the database won't affect other threads while it's in-progress
  2. The next test's data isn't polluted by prior tests
  3. This is about a zillion times faster than manually reloading data for each test.

I for one think this is pretty cool

Orion Edwards
A: 

I wanted to accept both Free Wildebeest's and Orion Edwards' answers but it would not let me. The reason I wanted to do this is that I'd come to the conclusion that these were the two main ways to do it, but which one to chose depends on the individual case (mostly the size of the database).

Garry Shutler
I always find that a problem on stackoverflow. It's the problem of choice when you really want to say that both are good.I guess if both are equally useful then they'll be voted up by similar numbers so stay near the top. Let the people decide :)
Free Wildebeest
A: 

Also run the tests at different times, so that they do not impact the performance or validity of each other.

Techboy
+1  A: 

Just to add to Free Wildebeest's answer I have also used HSQLDB to do a similar type testing where each test gets a clean instance of the DB.

Bradley Harris
A: 

While not as clever as the Rails unit test framework in one of the other answers here, creating distinct data per test or group of tests is another way of doing it. The level of tediousness with this solution depends on the number of test cases you have and how dependant they are on one another. The tediousness will hold true if you have one database per test or group of dependant tests.

When running the test suite, you load the data at the start, run the test suite, unload/compare results making sure the actual result meets the expected result. If not, do the cycle again. Load, run suite, unload/compare.