我只是想知道我是否可以使用Gatling验证JSON响应中某些filed的值?
目前代码只检查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中验证一些东西,也许可以参考那里的功能。只是好奇,我还是不知道为什么在之前的代码中使用assert不会失败,不过测试?