Scalatest中的方案大纲

pau*_*aul 1 scala cucumber scalatest

我正在使用scalatest实现我的测试框架,我认为使用该框架而不是Cucumber犯了一个错误

我正在尝试使用一些类似Scenario outline黄瓜的功能,以免造成DRY断裂

这是我的问题

  feature("Features of mus client") {
    scenario("GET message with mus client") {
      Given("a Musin message")
      val config: Properties = new Properties
      config.put("method", "POST")
      config.put("encoding", "UTF-8")
      config.put("uri", "http://localhost:9083/musClient")
      When("I make a request to f2e")
      val response = HttpClientTest.request(config, createJSON(READ))
      Then("The message it´s returned successfully")
      assert(response != null)
    }

    scenario("POST message with mus client") {
      Given("a Musin message")
      val config: Properties = new Properties
      config.put("method", "POST")
      config.put("encoding", "UTF-8")
      config.put("uri", "http://localhost:9083/musClient")
      When("I make a request to f2e")
      val response = HttpClientTest.request(config, createJSON(CREATE))
      Then("The message it´s returned successfully")
      assert(response != null)
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,我有两种情况,其中99%是相同的步骤,但是有一个变量会更改请求。

任何想法如何在scalatest中做到这一优雅高效

pra*_*upd 5

我也是选择scalatest黄瓜的人之一,黄瓜对我(ME)来说太多了,无法编写功能文件,然后回到scala / java文件并进行相应更改。维护两个文件。我实际上玩过cucumber javascala cucumber可能会更流利。无论如何,到目前为止,我对所有单元测试,组件测试和流测试都喜欢scalatest。

如果像您这样,如果属性在多个场景中是通用的,并且您不会在场景内部进行变异,则定义为通用属性就可以了,如下所示。

class E2E extends FeatureSpec with GivenWhenThen {
  feature("Features of mus client") {

    Given("http config")
    val config: Properties = new Properties(){{
        put("method", "POST") //you are doing POST in both case by the way
        put("encoding", "UTF-8")
        put("uri", "http://localhost:9083/musClient")
     }}

    scenario("GET message with mus client") {

      When("I make a request to f2e")
      val response = HttpClientTest.request(config, createJSON(READ))

      Then("The message it´s returned successfully")
      assert(response != null)
    }

    scenario("POST message with mus client") {

      When("I make a request to f2e")
      val response = HttpClientTest.request(config, createJSON(CREATE))

      Then("The message it´s returned successfully")
      assert(response != null)

    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,您可能还希望对更改的唯一部分使用基于属性的测试,基于属性的检查在spock框架中非常流畅并且可读。

在scalatest中基于属性的检查将如下所示,其中我正在测试两个不同的输入参数。(你import org.scalatest.prop.TableDrivenPropertyChecks._

class TestE2E extends FeatureSpec with GivenWhenThen {

  val requestResponse =
    Table(
      ("request", "response"),
      (  "GET",   "GET-something"),
      ( "POST",   "POST-something")
    )

  feature("testMe") {

    forAll (requestResponse) { (givenRequestFromTable: String, expectedResponseFromTable: String) =>

      scenario("for input " + givenRequestFromTable) {

        When("input is " + givenRequestFromTable)
        val output = testMe(input = givenRequestFromTable)

        Then("responseFromTable has something appended to it")
        assert(output == expectedResponseFromTable)
      }
    }
  }

  def testMe(input: String) : String = {
     input + "-something"
  }
}
Run Code Online (Sandbox Code Playgroud)

根据两个给定的属性,将有两种情况: 两次测试

对于您来说,基于属性的测试将如下所示,希望没有编译错误:)

import org.scalatest.prop.TableDrivenPropertyChecks._
import org.scalatest.prop.Tables.Table
import org.scalatest.{FeatureSpec, GivenWhenThen}

class PaulWritesSpecs extends FeatureSpec with GivenWhenThen {

  val requestResponse =
    Table(
      ("httpMethod", "requestType"),
      ("GET", READ),
      ("POST", CREATE))

  feature("Features of mus client") {

    forAll(requestResponse) { (httpMethod: String, requestType: String) => {

        scenario(s"$httpMethod message with mus client") {

          Given("http config")
          val config: Properties = new Properties() {{
            put("method", httpMethod)
            put("encoding", "UTF-8")
            put("uri", "http://localhost:9083/musClient")
          }}

          When("I make a request to f2e")
          val response = HttpClientTest.request(config, createJSON(requestType))

          Then("The message it´s returned successfully")
          assert(response != null)
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)