2017-02-22 42 views
1

I'm實現我的測試框架,我認爲我使用這個框架來代替黃瓜方案提綱scalatest使用scalatest

我正嘗試使用某種功能作爲黃瓜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%是it's變更請求的相同的步驟,但一個變量。

任何想法如何做到這一點優雅和高效的scalatest

回答

1

而我那些過於誰在黃瓜選擇scalatest之一,黃瓜是太多,我(ME)寫的特徵文件,然後回到scala/java文件並相應地改變。保持兩個文件。我其實玩過cucumber 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) 

    } 
    } 
} 

但是,你也可能要使用property based testing關於改變,基於屬性的檢查是非常流暢,可讀性於spock framework的唯一部分。

基於屬性的scalatest檢查看起來像下面我正在測試兩個不同的輸入參數。 (您neeed 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" 
    } 
} 

會有基於兩個性能給出兩種方案, two tests

和爲貴,測試將與財產的東西如下爲主,希望有沒有編譯錯誤: )

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