【问题标题】:How to create test fixtures for every example of a functional test如何为功能测试的每个示例创建测试夹具
【发布时间】:2019-06-01 09:07:52
【问题描述】:

这里是 Scala 菜鸟。

我目前尝试使用 specs2 为基于 Play (Scala) 的 Web 应用程序创建功能测试。示例本身很简单,即:

class SignInSpec extends PlaySpecification {

  // bring the workflow steps in scope and instrument browser with them    
  import utils.WorkflowSteps.AuthSteps

  "An activated user".should {
    "be able to sign in to the admin console" in new WithDbData(webDriver = WebDriverFactory(FIREFOX)) {

      // this should be: browser.signIn(testData.manager)
      // with the manager already persisted in the database
      browser.signIn(Manager(None, "me@home.com", "12341234", "John", "Doe", List("admin")))
    }
  }
}

我想要实现的是为每个示例提供一组已定义的测试数据,其中一些已经保存在数据库中。因此,我需要围绕每个示例进行设置和拆卸方法,准备一个TestData 案例类,用适当的数据填充它并保留其中的一些数据,以便示例可以从定义的数据库状态开始。 最终,我想要一个插件机制,其中插件为一组示例定义测试数据(将其视为贷款模式的应用)。

到目前为止我做了什么:

  • 我尝试使用 Around 的一些风格,但我不知道如何将数据输入示例,因为我必须添加额外的返回值。
  • 我尝试了 specs2 的 ForEach 上下文,但这与 Play 的 WithBrowser 冲突
  • 我玩过隐式 val,但我还是不知道如何将隐式参数添加到使用 DelayedInit 从构造函数转换为函数调用参数的块中

任何想法如何继续实现以下目标?

  • 从使用单个参数TestData 调用示例的附加特征或类扩展规范或示例
  • 这个额外的特征或类应该能够准备测试数据并保留其中的一部分
  • 此附加特征或类应与WithBrowser 兼容

【问题讨论】:

    标签: scala playframework functional-testing specs2


    【解决方案1】:

    一种方法是使用所谓的“贷款固定装置”:

         def withTestData(test: TestData => Unit) = {
           val data = setupTestData() 
           try { 
             test(data)
           } finally {
             destroyTestData(data)
           }
         }
    
    
    
        "An activated user" should "be able to sign in to the admin console" in withTestData { data =>
           new WithDbData(webDriver = WebDriverFactory(FIREFOX)) {
             browser.signIn(data.manager)
           }
         }
    

    等等..

    【讨论】:

    • 谢谢你的回答,迪玛。到目前为止,我已经让它工作了,我现在可以创建和保存测试数据,并将它交给示例。但是仍然存在一个问题:如果准备测试数据失败(即数据库不可用),测试结果仍然是肯定的,尽管它应该是一个错误。你知道如何实现吗?
    【解决方案2】:

    一种解决方案可能如下:

     case class TestData(manager: Manager) extends WithDbData(webDriver = WebDriverFactory(FIREFOX))
    
    
     class SignInSpec extends PlaySpecification with ForEach[TestData] {
    
       // bring the workflow steps in scope and instrument browser with them    
       import utils.WorkflowSteps.AuthSteps
    
       "An activated user".should {
         "be able to sign in to the admin console" in { data: TestData =>
            import data._
    
            browser.signIn(manager)
         }
       }
    
       def foreach[R : AsResult](f: TestData => R): Result = ???
     }
    

    这是以必须 import data._ 为代价的,但这也避免了之前解决方案的双重嵌套。

    【讨论】:

    • 谢谢你的回答,埃里克。这会起作用,但是让数据类实现测试行为(WithDbData extends WithBrowser)相当尴尬。不过,嵌套是一个问题,我仍在努力解决。
    • 不一定是case类,可以是class TestData(val manager: Manager)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多