【问题标题】:Validate JSON in Gatling performance test在 Gatling 性能测试中验证 JSON
【发布时间】:2020-05-22 11:43:06
【问题描述】:

我只是想知道是否可以使用 Gatling 验证 JSON 响应中某些字段的值?

目前代码仅检查 JSON 响应中是否存在字段,如下所示:

val searchTransaction: ChainBuilder = exec(http("search Transactions")
    .post(APP_VERSION + "/transactionDetals")
    .headers(BASIC_HEADERS)
    .headers(SESSION_HEADERS)
    .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
    .check(status.is(200))
    .check(jsonPath("$.example.current.transaction.results[0].amount.value")

如果我想验证交易价值等于0.01,是否可以做到这一点? 我google了一下,没有找到,如果之前有类似的问题,请告诉我我会关闭这个。谢谢。

我在测试中尝试了一些断言,我发现断言根本不会失败。

val searchTransaction: ChainBuilder = exec(http("search Transactions")
    .post(APP_VERSION + "/transactionDetals")
    .headers(BASIC_HEADERS)
    .headers(SESSION_HEADERS)
    .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
    .check(status.is(200))
    .check(jsonPath("$.example.current.transaction.results[0].amount.value").saveAs("actualAmount"))).
    exec(session => {
    val actualTransactionAmount = session("actualAmount").as[Float]
    println(s"actualTransactionAmount: ${actualTransactionAmount}")
    assert(actualTransactionAmount.equals(10)) // this should fail, as amount is 0.01, but performance test still pass.
    session
  })

【问题讨论】:

    标签: json scala performance-testing scala-gatling


    【解决方案1】:

    没错,这是正常的解决方案

    .check(jsonPath("....").is("...")))
    

    这样的检查是常态。由于服务可能会响应而不返回例如 5xx 状态,但响应中会出现错误。所以最好检查一下。

    示例:我有一个返回创建用户状态的应用程序并检查它

    .check(jsonPath("$.status").is("Client created"))
    

    【讨论】:

      【解决方案2】:

      我想出了验证方法,不确定这是否是最好的方法,但它对我有用。

      val searchTransaction: ChainBuilder = exec(http("search Transactions")
          .post(APP_VERSION + "/transactionDetals")
          .headers(BASIC_HEADERS)
          .headers(SESSION_HEADERS)
          .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
          .check(status.is(200))
          .check(jsonPath("$.example.current.transaction.results[0].amount.value").is("0.01")))
      

      如果我将值更改为 0.02,那么测试将失败,并且会在会话日志中显示 如下所示:

      ---- Errors --------------------------------------------------------------------
      > jsonPath($.example.current.transaction.results[0].amount.value).     19 (100.0%)
      find.is(0.02), but actually found 0.01
      ================================================================================
      

      我知道验证 JSON 中的值会使其成为功能测试,而不是负载测试。在负载测试中,也许我们不应该验证每一条可能的信息。但是如果有人想在 JSON 中验证某些东西,那么那里有一个功能,也许可以参考。 只是好奇,我仍然不知道为什么在之前的代码中使用 assert 不会失败?

      【讨论】:

        猜你喜欢
        • 2018-02-03
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多