绝对是的,我正在为 unit-testing、integration-testing、component-testing 和 shakedown-testing 我的微服务使用 scalatest。
没有使用太多JMeter,我尝试了它来测试我的微服务,对我来说有点沮丧,立即放弃了。
Scalatest 对我来说非常高效f#*$_&g。
一个非常简单的集成测试示例,(我使用 https://jsonplaceholder.typicode.com/posts/1 作为我的 API 端点,即在线 API)
class SomeHttpFlowSpecs extends HttpFlowSpecs {
feature("Getting user posts on my API server") {
scenario("As a software engineer, I want to receive the user posts when I call the endpoint") {
When("I send a GET request to the http endpoint")
val response = doHttpGet("https://jsonplaceholder.typicode.com/posts/1")
Then("I receive 200 response back with the user posts")
assert(response.getStatusLine.getStatusCode == 200)
val expectedJson =
"""
|{
| "userId": 1,
| "id": 1,
| "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
| "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
|}
""".stripMargin
assert(JSON.parseRaw(responseContent(response)) == JSON.parseRaw(expectedJson))
}
}
}
我编写了自己的HttpFlowSpecs 作为用于进行 http 调用和处理响应的 Util。与您所说的 Future 响应不同,这些呼叫与我是同步的。我正在使用众所周知的apache httpclient 进行http 通信。
class HttpFlowSpecs extends extends FeatureSpec with GivenWhenThen with BeforeAndAfterAll {
def doHttpGet(endpoint: String): CloseableHttpResponse = {
val httpRequest = new HttpGet(endpoint)
val response = (new DefaultHttpClient).execute(httpRequest)
response
}
def doHttpPost(endpoint: String, content: String, contentType: String = "application/json"): CloseableHttpResponse = {
val httpRequest = new HttpPost(endpoint)
httpRequest.setHeader("Content-type", contentType)
httpRequest.setEntity(new StringEntity(content))
val response = (new DefaultHttpClient).execute(httpRequest)
response
}
def responseContent(response: CloseableHttpResponse): String = {
val responseBody: String = new BufferedReader(new InputStreamReader(response.getEntity.getContent))
.lines().collect(Collectors.joining("\n"))
println("Response body = " + responseBody)
responseBody
}
}
这是一个非常简单的conifers-spec,我用于测试。
我将pegdown - Java Markdown processor 与scalatest 一起用于html 报告,如下所示,
此外,您可以以非常易读的格式查看所有场景,查看我的项目中的一个组件测试,
我如何运行测试
mvn clean test
希望这很有用,如果有任何问题,请告诉我,很乐意提供帮助。