views:

60

answers:

3

I want to implement integration tests of my Entity Framework driven repositories. The problem is how to rollback database state after tests are done. At the moment I'm planning to start transaction at test SetUp and roll it back at test TearDown. Are there any other solutions excepting manual database clearing?

+1  A: 

That's probably the easiest way, the other way is to rebuild the database at SetUp.

Rob Fonseca-Ensor
+1  A: 

I think you're on the right track....

Here's an example doing the same with Linq To SQL that you can tweek for yourself.

This link describes three options:

  • Transactions
  • Rebuild the DB
  • Use SQL Server Snapshots

The post goes on to describe that transactions while the fastest are tied to a single session and can create some real problems/restrictions. Use if you can....

Rebuilding the DB is slow but definitely doable but using snapshots is fast and gets around the transaction restrictions.

If you have a need to have very high performance in your automated tests try this from the same blogger. He describes using MS Distributed Transaction Coordinator to eliminate the transactional restrictions of a single session.

klabranche
+2  A: 

We do this in our integration tests while using MSTest. We use the TransactionScope and implement a test setup and teardown in a base class. This allows you to run all integration tests within a transaction. The base class looks much like this:

public class IntegrationTestsBase
{
    private TransactionScope scope;

    [TestInitialize]
    public void Initialize()
    {
        this.scope = new TransactionScope();
    }

    [TestCleanup]
    public void TestCleanup()
    {
        this.scope.Dispose();
    }
}

Good luck.

Steven
Very nice! Now if only it worked for Oracle...
Cylon Cat
As far as I know the `TransactionScope` works with Oracle. It is specially designed for multi-server multi-vendor (two-phase commit) communication.
Steven