在 scalatest 中概述场景
Scenario outline in scalatest
我正在使用 scalatest 实现我的测试框架,我认为我使用这个框架而不是 Cucumber 犯了一个错误
我正在尝试使用黄瓜 Scenario outline
的某些功能来避免断干
这是我的问题
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 中优雅而高效地做到这一点吗
我也是那些选择 scalatest
而不是黄瓜的人之一,黄瓜对我(我)来说太多了,无法编写功能文件,然后回到 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 for the only part that changes, property based check was very fluent and readable in spock framework。
属性 基于 scalatest 的检查如下所示,我正在测试两个不同的输入参数。 (你需要 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"
}
}
根据两个给定的属性会有两种情况,
对于你来说,测试将是基于 属性 的,希望没有编译错误:)
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 实现我的测试框架,我认为我使用这个框架而不是 Cucumber 犯了一个错误
我正在尝试使用黄瓜 Scenario outline
的某些功能来避免断干
这是我的问题
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 中优雅而高效地做到这一点吗
我也是那些选择 scalatest
而不是黄瓜的人之一,黄瓜对我(我)来说太多了,无法编写功能文件,然后回到 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 for the only part that changes, property based check was very fluent and readable in spock framework。
属性 基于 scalatest 的检查如下所示,我正在测试两个不同的输入参数。 (你需要 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"
}
}
根据两个给定的属性会有两种情况,
对于你来说,测试将是基于 属性 的,希望没有编译错误:)
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)
}
}
}
}
}