【问题标题】:Scala: Acceptance Testing OrderScala:验收测试订单
【发布时间】:2015-07-12 04:45:16
【问题描述】:

供参考:How make tests always run in same order in Scalatest?

我计划通过调用控制器/路由并将响应与预期的响应进行比较来测试我的应用程序。

我不想模拟我的持久层,所以我也可以测试它。我现在的方法是执行测试以反映用户操作。示例:

Test 1: User registers 
--> Test 2: (depends on a existing user) User creates profile 
--> Test 3: (depends on a user with existing profile) User changes profile

所以为了节省时间,我不想为测试 2 和测试 3 模拟任何东西,而是一直在同一个数据库上工作,并使用前面测试生成的数据。

这种方法可以吗?如何在 Specs2 或 ScalaTest 中指定执行顺序?

【问题讨论】:

    标签: scala bdd playframework-2.3 scalatest specs2


    【解决方案1】:

    最好在单个测试套件之间不存在依赖关系,至少有两个原因:

    • 关注套件的执行顺序会使测试执行更难理解
    • 如果套件A 依赖于套件B,则更改套件B 中的某些内容可能会破坏套件A,这意味着更难找到测试失败的原因。

    由于这些缺点,我建议您在每次验收测试开始时正确设置持久层;以牺牲执行时间为代价。请注意,您可以tag your tests 并且只偶尔执行缓慢的验收测试,以免减慢您的开发周期。

    如果你想在 ScalaTest 中实现依赖测试,你可以按照你链接的问题中的建议创建一个嵌套测试套件:

    假设你的持久层:

    object Users {
      var users: List[User] = Nil
    
      def apply(i: Int): User = users(i)
    
      def register(user: User): Unit = users = user :: users
    
      def isEmpty: Boolean = users.isEmpty
    }
    
    class User(var profile: Option[Profile] = None) {
      def createProfile(): Unit = profile = Some(new Profile)
    }
    
    class Profile(var content: String = "") {
      def update(newContent: String): Unit = content = newContent
    }
    

    以及您的个人测试:

    @DoNotDiscover
    class Test1 extends FlatSpec with ShouldMatchers {
      "register" should "store a new user" in {
        Users.register(new User)
    
        Users should not be 'empty
      }
    }
    
    @DoNotDiscover
    class Test2 extends FlatSpec with ShouldMatchers {
      "createProfile" should "create a new user profile" in {
        val user = Users(0)
        user.createProfile()
    
        user.profile shouldBe 'defined
      }
    }
    
    @DoNotDiscover
    class Test3 extends FlatSpec with ShouldMatchers {
      "update" should "update the content of the profile" in {
        val newContent = "Test"
        val profile = Users(0).profile.get
        profile.update(newContent)
    
        profile.content shouldBe newContent
      }
    }
    

    您可以将它们嵌套在验收测试套件中:

    class AcceptanceTests extends Suites(
      new Test1,
      new Test2,
      new Test3
    ) with SequentialNestedSuiteExecution
    

    @DoNotDiscover 注释对于防止测试运行器单独执行嵌套测试是必要的(因为它们本身就是测试套件)。混入 trait SequentialNestedSuiteExecution 保证嵌套测试按给定顺序执行。

    【讨论】:

    • 虽然我自己没用过,但也许FitNesse,一个专门的集成/验收测试框架比ScalaTest更适合你的需求。
    • 感谢您的回答。我理解你的推理。当我可以重用现有测试,将它们视为我的设置功能时,我自己做持久性固定装置似乎是错误的......我很欣赏示例代码!
    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2010-10-18
    • 1970-01-01
    • 2015-10-17
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多