比较脚本断言中的 JSON 个响应

Compare JSON Responses in Script Assertation

我正在尝试使用以下 Groovy 脚本比较来自两个单独测试步骤的两个 JSON 响应以确定它们是否完全相等(成功的情况意味着它们是):

def response1 = context.expand( '${GetPatientProfileById#Response#}' )
def response2 = context.expand( '${GetPatientProfileById#Response2#}' )
log.info(response1)
log.info(response2)
assert response1 == response2

如何始终表示通过和returns以下信息:

Mon Oct 05 11:41:33 CDT 2015:INFO:
Mon Oct 05 11:41:33 CDT 2015:INFO:

我错过了什么?我的印象是 response1 和 response2 会从各自的测试步骤的响应中保存 JSON 字符串值,但我显然遗漏了一些东西。

由于最后一个 #,您的 test step 中的 GetPatientProfileById 没有得到 response 属性。

这就是 context.expand( '${GetPatientProfileById#Response#}' ) 返回空白的原因。要更正它,请删除最后一个 #,如下所示:context.expand( '${GetPatientProfileById#Response}' ).

还有@SiKing 的评论请注意,您将获得两个变量的相同测试步骤响应。

希望这对您有所帮助,

这是我最终使用的:

import groovy.json.JsonSlurper

responseContent = messageExchange.modelItem.testCase.getTestStepByName("TestStepName").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)

responseContent2 = messageExchange.modelItem.testCase.getTestStepByName("TestStepName2").getPropertyValue("response")
slurperresponse2 = new JsonSlurper().parseText(responseContent)

log.info (slurperresponse)
log.info (slurperresponse2)

assert slurperresponse == slurperresponse2