【问题标题】:What could be best approach for regression suite for microservices, Can Scala be used for that?微服务回归套件的最佳方法是什么,Scala 可以用于此吗?
【发布时间】:2017-07-31 09:02:17
【问题描述】:

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

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

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

【问题讨论】:

  • 如果你想用 Scala 做性能回归,你可以使用 Gatling gatling.io

标签: scala testing junit jmeter scalatest


【解决方案1】:

绝对是的,我正在为 我的微服务使用 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

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

【讨论】:

  • 这真的很有帮助,但看起来我需要重新构建我一直在写的内容。对于 http 调用,我遵循 Play WS,并正在启动 Akka Actor 系统并为 http 请求创建 WS 客户端,但这些是异步调用,这让我想知道,至于组件/集成级别测试,我需要调用一组 API异步调用很难获得响应和利用它们。让我知道是否能以某种方式与您取得联系,这对我真的很有帮助。
  • 您可以考虑使用AsyncFeatureSpec 进行异步断言,这对于这种情况非常酷。这样您就不必手动等待响应,example here。在此示例中,我使用 akka-http 作为客户端。从概念上讲,它类似于 PlayWSclient。 email mecreate issue 什么都舒服。如果可以的话,我会尽力提供帮助。除非我陷入生产问题:)
【解决方案2】:

我宁愿选择JMeter,主要特点是:

  • 开箱即用,您拥有所需的一切(采样器、前/后处理器、断言、reporting 等)
  • JMeter 测试可以scaled 在多台机器上运行
  • 可以使用 GUI 创建 JMeter 测试,无需编程语言知识

还有Gatling 工具,其中使用基于Scala 的DSL 创建测试,但是根据Open Source Load Testing Tools: Which One Should You Use?,JMeter 在协议支持、可扩展性和吞吐量方面更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2019-01-08
    • 1970-01-01
    • 2020-06-14
    • 2018-11-23
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多