在 Gatling 性能测试中验证 JSON
Validate JSON in Gatling performance test
我只是想知道我是否可以使用 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
})
我想出了验证的方法,不确定这是不是最好的方法,但它对我有用。
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中的某些内容,也许可以参考。
只是好奇,我仍然不知道为什么在以前的代码中使用断言不会通过测试?
没错,这是正常的解决方案
.check(jsonPath("....").is("...")))
这样的检查是常态。由于服务可能会响应而不是 return 例如 5xx 状态,但响应中会出现错误。所以最好检查一下。
示例:我有一个 return 创建用户状态的应用程序,我检查了它
.check(jsonPath("$.status").is("Client created"))
我只是想知道我是否可以使用 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
})
我想出了验证的方法,不确定这是不是最好的方法,但它对我有用。
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中的某些内容,也许可以参考。 只是好奇,我仍然不知道为什么在以前的代码中使用断言不会通过测试?
没错,这是正常的解决方案
.check(jsonPath("....").is("...")))
这样的检查是常态。由于服务可能会响应而不是 return 例如 5xx 状态,但响应中会出现错误。所以最好检查一下。
示例:我有一个 return 创建用户状态的应用程序,我检查了它
.check(jsonPath("$.status").is("Client created"))