gpt4 book ai didi

scala - 什么是微服务回归套件的最佳方法,Scala 可以用于此吗?

转载 作者:行者123 更新时间:2023-11-28 21:04:14 25 4
gpt4 key购买 nike

我被要求准备一个用于测试微服务的回归套件,为此我需要:- 运行脚本- 运行不同的 API(对于下一个 API 中使用的场景可以是多个)- 以某种可呈现的格式发布结果

我自己的第一选择是 Jmeter,但高级资源不同意,我被要求使用 SCALA,而在使用 Scalatest 时,我坚持使用 Futures,因为我的大部分测试都围绕着调用 Restful API。

我仍然认为 Jmeter 更适合我正在做的事情,但没有坚实的基础,有人可以建议我应该选择哪一个吗?

最佳答案

绝对是,我正在为 使用 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 报告,如下所示,

pegdown

此外,您可以以非常易读的格式查看所有场景,在我的项目中查看我的一个组件测试

reports

我如何运行测试

mvn clean test

希望这是有用的,如果有任何问题,请告诉我,我们很乐意提供帮助。

关于scala - 什么是微服务回归套件的最佳方法,Scala 可以用于此吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42711241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com