2017-02-22 67 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) 
     } 
     } 
    } 
    } 
}