最新的情景大纲

时间:2017-02-22 08:11:04

标签: 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)
    }

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

任何想法如何在最新的

中做到这一优雅和高效

1 个答案:

答案 0 :(得分:5)

我也是其中一个选择console.log(allData['trending'])而非黄瓜的人,黄瓜对我(ME)来说太多了,无法编写功能文件,然后回到scala / java文件并相应地更改。保留两个文件。我实际上玩scalatestcucumber java可能更流畅。无论如何,到目前为止,我对所有的单元测试,元件测试和流量测试都很喜欢。[/ p>

如果属于您的情况,如果属性在多个场景中很常见,并且您不会在场景内变异,那么定义为公共属性就没问题,如下所示。

scala cucumber

但是,您可能还希望property based testing仅用于更改,基于属性的检查在spock framework中非常流畅和可读。

在palatest中基于属性的检查看起来如下所示我在测试两个不同的输入参数。 (你需要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) } } }

import org.scalatest.prop.TableDrivenPropertyChecks._

基于两个给定属性会有两种情况, two tests

对你而言,测试将基于属性,如下所示,希望没有编译错误:)

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