【问题标题】:Doing a rollback - Repository integration tests进行回滚 - 存储库集成测试
【发布时间】:2010-08-21 14:57:46
【问题描述】:

我想对我的 Entity Framework 驱动的存储库实施集成测试。问题是如何在测试完成后回滚数据库状态。目前我计划在测试SetUp 开始事务并在测试TearDown 回滚。除了手动清库,还有其他解决方案吗?

【问题讨论】:

    标签: c# .net database entity-framework


    【解决方案1】:

    我们在使用 MSTest 时在集成测试中执行此操作。我们使用TransactionScope 并在基类中实现测试设置和拆卸。这允许您在事务中运行所有集成测试。基类看起来很像这样:

    public class IntegrationTestsBase
    {
        private TransactionScope scope;
    
        [TestInitialize]
        public void Initialize()
        {
            this.scope = new TransactionScope();
        }
    
        [TestCleanup]
        public void TestCleanup()
        {
            this.scope.Dispose();
        }
    }
    

    祝你好运。

    【讨论】:

    • 据我所知,TransactionScope 适用于 Oracle。专为多服务器多厂商(两阶段提交)通信而设计。
    • 好的,在测试中执行 repo.Save(someObject) 之后,您如何检查该对象是否已保存?
    • @Peri 你创建了 TransactionScope ala "return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions{IsolationLevel = IsolationLevel.ReadUncommitted})。这样你就可以看到未提交的行:-)
    • @Peri:或者您只是在该测试中查询数据库。由于您在同一事务中运行,因此应该可以。没有什么特别需要的。
    • 是否有可能某些错误只有在我们在测试中不做的提交后才会可见?例如一些约束检查失败等等。它是否独立于数据库引擎(SQL Server、Oracle、MySQL ...)?如果我们正在测试我们的类可以读取数据怎么办。他们使用 ReadCommited 隔离级别?
    【解决方案2】:

    我认为你在正确的轨道上......

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

    This link describes three options:

    • 交易
    • 重建数据库
    • 使用 SQL Server 快照

    该帖子继续描述了最快的事务与单个会话相关联,并且可能会产生一些实际问题/限制。如果可以,请使用....

    重建数据库很慢,但绝对可行,但使用快照速度很快,并且可以绕过事务限制。

    如果您需要在自动化测试中获得非常高的性能try this from the same blogger。他描述了使用 MS 分布式事务协调器来消除单个会话的事务限制。

    【讨论】:

      【解决方案3】:

      在 Setup 中打开 TransactionScope 并在 TearDown 中处理的问题是您没有测试提交!

      【讨论】:

        【解决方案4】:

        这可能是最简单的方法,另一种方法是在SetUp重建数据库。

        【讨论】:

          【解决方案5】:

          最好的方法是交易方法。我提供的链接包含一个简短的演练。我接触过的几乎所有企业解决方案都使用基于事务的方法。确保还查看文章底部的链接,其中包含指向 Microsoft 与实体框架交易的文档的链接。上面列出的其他选项在清理测试事务的简单概念中竞争过大。构建数据库或使用服务器快照对于这个问题来说完全是矫枉过正。 TransactionScope 甚至不执行未完成集成测试的事务。

          实现交易

          这将在每个测试开始之前创建一个事务,并在每个测试结束后回滚该事务。

          [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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-20
            • 2016-08-21
            • 2011-11-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-11
            • 1970-01-01
            相关资源
            最近更新 更多