开发者

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?


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.


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.


The problem with opening TransactionScope in Setup and Disposing in TearDown is that You are NOT testing the commit!


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


The best way is a transactional approach. The link I provided contains a short walk through. Almost every enterprise solution I've come in contact with use a transactional based approach. Make sure to also take a look at the links on the bottom of the article which have links to Microsoft's Documentation on transactions with entity framework. The other options listed above are compete overkill in a simple concept of cleaning up a test transaction. Building a database or using sever snapshots are complete overkill to this issue. TransactionScope doesn't even execute the transaction leaving an integration test unfinished.

Implement Transactions

This will create a transaction before each tests starts and rollback the transaction after each test ends.

[TestClass]
public class TransactionTest
{
  protected EntitiesV3 context;
  protected DbContextTransaction transaction;

  [AssemblyInitialize]
  public static void AssemblyStart(TestContext testContext)
  {
    RetryDbConfiguration.SuspendExecutionStrategy = true;
  }

  [TestInitialize]
  public void TransactionTestStart()
  {
    context = new EntitiesV3();
    transaction = context.Database.BeginTransaction();
  }

  [TestCleanup]
  public void TransactionTestEnd()
  {
    transaction.Rollback();
    transaction.Dispose();
    context.Dispose();
  }

  [AssemblyCleanup]
  public static void AssemblyEnd()
  {
    RetryDbConfiguration.SuspendExecutionStrategy = false;
  }
}

Great quick walk through on transactional rollback/cleanup approach

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜