Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing a rollback - Repository integration tests

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?

like image 338
SiberianGuy Avatar asked Aug 21 '10 14:08

SiberianGuy


People also ask

Which of these annotations will you use for automatically rolling back a transaction after a test?

Annotation Type Rollback@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.

Can integration testing be done manually?

In most cases, integration testing doesn't require any specific tools. A QA team often runs these tests manually. Usually, it happens in parallel with the development – that's probably the most efficient timing.


2 Answers

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.

like image 71
Steven Avatar answered Oct 07 '22 12:10

Steven


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.

like image 33
Kevin LaBranche Avatar answered Oct 07 '22 12:10

Kevin LaBranche