2017-02-22 3 views
1

미안은 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 %가 요청을 변경 동일한 단계하지만 변수를 키우면 두 가지 시나리오를 볼 수 있듯이.

이 우아하고 효율적인 작업을 수행하는 방법에 대해 어떤 생각 scalatest에서

답변

1

그리고 오이에 걸쳐 scalatest를 선택 너무 사람들의 일이야, 오이는 기능 파일을 작성하는 나 (ME) 너무 많이 한 후, scala/java 파일로 돌아와서 그에 맞게 변경하십시오. 두 개의 파일을 유지 보수하십시오. 실제로 cucumber java을 연주 했으니 scala cucumber이 더 유창 할 것입니다. 어쨌든, 지금까지 모든 유닛 테스트, 컴포넌트 테스트 및 플로우 테스트에 대해 스칼라 테를 좋아합니다.

속성이 여러 시나리오에서 공통적 인 경우 내부 시나리오를 변경하지 않으면 공통 속성으로 정의하는 것이 좋을 것입니다.

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) 

    } 
    } 
} 

그러나,는 또한 재산 기반 검사 spock framework 매우 유창하게 읽을 수 있었다 만 변경 부분에 대한 property based testing을 사용할 수 있습니다.

scalatest의 속성 기반 검사는 다음과 같이 두 개의 다른 입력 매개 변수를 테스트합니다. (당신이 import org.scalatest.prop.TableDrivenPropertyChecks._을 neeed)

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