【问题标题】:ScalaTest: understanding the order of execution of testsScalaTest:了解测试的执行顺序
【发布时间】:2014-09-12 23:02:23
【问题描述】:

我很难理解为什么我的 setup-teardown 程序没有像我在 ScalaTest 中所期望的那样工作。基本上,我的测试代码如下所示:

class UserManagerTest extends FlatSpec with ManagerBehaviors[User, UserDAO] {

  "An entity manager" should behave like allFindingManager(UserManager)

  "An entity manager" should behave like countingManager(UserManager)
}


trait ManagerBehaviors[T <: IEntity, D <: AbstractDAO[T]] {

this: FlatSpec =>

   private def withClearDB(entityManager: BasicManager[T, D], testFunction: () => Unit) = {
    try {
      clearDB(entityManager)
      testFunction()
    } finally {
      clearDB(entityManager)
    }
  }

  private def clearDB(manager: BasicManager[T, D]) = {
    manager.all match {
      case Some(entities) => for (entity <- entities) manager.remove(entity.getId)
    }
  }

  def allFindingManager(manager: BasicManager[T, D]) {
    withClearDB(manager,
      () => it should "properly return all elements in the database for the entity" in {

        // Add 10 entities to the database
        for (i <- 1 to 10) manager.persist(manager.defaultEntity)

        manager.all match {
          case Some(entities) => assert(entities.length == 10)
          case None => fail()
        }
      })
  }

  def countingManager(manager: BasicManager[T, D]) {
    withClearDB(manager,
      () => it should "properly count the number of entities managed" in {

        // Add 10 entities to the database
        for (i <- 1 to 10) manager.persist(manager.defaultEntity)

        assert(manager.count == 10)
      })
  }
}

我期望的是以下顺序:

  1. 清除数据库

  2. allFindingManager

  3. 清除数据库

  4. 清除数据库

  5. countingManager

  6. 清除数据库

单独运行时,测试按预期工作 - 数据库在启动时被清除,而在测试结束时为空。

但是,在运行整个 UserManagerTest 类时,由于某种原因,它会失败并显示以下内容:

org.scalatest.exceptions.TestFailedException: 20 did not equal 10

这样做的原因是,不知何故,测试是在没有间歇性的 clearDB 调用的情况下一个接一个地调用的。我不知道为什么会这样。

当使用调试器并在finally 块内放置断点时,可能会发现正在发生的事情的线索:该块确实像预期的那样运行了两次,但这发生在执行测试方法之前(!) ,这根本没有意义:

  1. 清除数据库

  2. 清除数据库

  3. 清除数据库

  4. 清除数据库

  5. allFindingManager

  6. countingManager

谁能帮我解释一下?

【问题讨论】:

  • 我注意到in the doc section "calling loan-fixture methods" 的一件事是您将withClearDb 包装在should 子句之外,但文档使用成语"something" should "do something" in withClearDb {。也许你正以这种方式陷入 DSL 机制的齿轮中。
  • 不确定是否有帮助,但我在处理类似问题时最终使用了 BeforeAndAfter 特征

标签: scala scalatest


【解决方案1】:

如果你改变了

def countingManager(manager: BasicManager[T, D]) {
  withClearDB(manager,
    () => it should "properly ..." in {

      // Add 10 entities to the database
      for (i <- 1 to 10) manager.persist(manager.defaultEntity)

      assert(manager.count == 10)
    })
}

def countingManager(manager: BasicManager[T, D]) {

  it should "properly ..." in withClearDB(manager,
    () => {
      // Add 10 entities to the database
      for (i <- 1 to 10) manager.persist(manager.defaultEntity)

      assert(manager.count == 10)
    }
  )

}

同样在另一个函数中,执行顺序将如您所愿。

【讨论】:

  • 确实,我实际上只是要回来请您将您的评论放入答案中,因为这解决了问题。我看到你打败了我,请接受 :) 非常感谢。
猜你喜欢
  • 2013-08-16
  • 2014-09-23
  • 2022-01-23
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2012-10-14
  • 2021-06-22
相关资源
最近更新 更多