2015-10-05 24 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評論注意到你得到了你的兩個變量相同的測試步驟響應。

希望這有助於