在加特林性能测试中验证 JSON



>我只是想知道我是否可以使用 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,是否可以实现此目的? 我用谷歌搜索但没有找到任何结果,如果之前有类似的问题,请告诉我我会关闭它。谢谢。

我在测试中尝试了一些断言,我发现断言根本不会降低性能。

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
})

你说得对,这是一个正常的解决方案

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

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

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

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

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

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 中的某些内容,那里有功能,也许可以参考。 只是好奇,我仍然不知道为什么在以前的代码中使用断言不会通过测试?

最新更新