Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scalatest中的场景大纲_Scala_Cucumber_Scalatest - Fatal编程技术网

scalatest中的场景大纲

scalatest中的场景大纲,scala,cucumber,scalatest,Scala,Cucumber,Scalatest,我正在使用scalatest实现我的测试框架,我认为我在使用这个框架而不是Cucumber时犯了一个错误 我试图使用一些特性作为黄瓜的场景大纲,以避免干裂 这是我的问题 feature("Features of mus client") { scenario("GET message with mus client") { Given("a Musin message") val config: Properties = new Properties

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

我试图使用一些特性作为黄瓜的场景大纲,以避免干裂

这是我的问题

  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)
    }
正如您所看到的,我有两个场景,其中99%是相同的步骤,但一个变量改变了请求


我也是选择scalatest而不是cucumber的人之一,cucumber对于meME来说太难了,无法编写功能文件,然后返回scala/java文件并进行相应更改。维护两个文件。我实际上玩过黄瓜爪哇,scala 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)

    }
  }
}
但是,您可能还希望对唯一发生更改的部分使用基于属性的检查,该检查在中非常流畅且可读

基于属性的签入scalatest如下所示,我正在测试两个不同的输入参数。您需要导入org.scalatest.prop.TableDrivenPropertyChecks_

基于两个给定属性,将有两种情况,

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

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)
        }
      }
    }
  }
}

我也是选择scalatest而不是Cumber的人之一,Cumber对于meME来说太难了,无法编写功能文件,然后返回scala/java文件并进行相应的更改。维护两个文件。我实际上玩过黄瓜爪哇,scala 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)

    }
  }
}
但是,您可能还希望对唯一发生更改的部分使用基于属性的检查,该检查在中非常流畅且可读

基于属性的签入scalatest如下所示,我正在测试两个不同的输入参数。您需要导入org.scalatest.prop.TableDrivenPropertyChecks_

基于两个给定属性,将有两种情况,

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

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)
        }
      }
    }
  }
}