2015-10-05 22 views
0

我试图比较两个单独的测试步骤生成两个JSON响应,以确定它们是否正好彼此相等(成功的情况下,意味着它们)使用以下Groovy脚本:在脚本Assertation比较JSON响应

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

如何,只是总是预示着通并返回以下信息:

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

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

+0

什么类没有context.expand返回?如果它不是一个字符串,那么'response1 == response2'就是比较两个不同的对象。尝试'response1.toString()== response2.toString()' – Dan

+0

什么是'GetPatientProfileById'? ** **'response1'和'response2'正在使用相同的测试步骤! – SiKing

+0

@SiKing修好了! – Pseudonym

回答

2

这是我最终使用:

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 
+1

你仍然在比较**自己的同一个**响应! – SiKing

+1

同意SiKing。可能是复制粘贴的问题。 – Rao

+0

arg对不起,这是一个盲目的复制粘贴,纠正问题! – Pseudonym

1

由于最近的#,您没有收到test stepresponse属性GetPatientProfileById

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

此外,作为@SiKing评论注意到你得到了你的两个变量相同的测试步骤响应。

希望这有助于