SoapUI如何验证json字符串是一个数组,所有元素都是相同的值

时间:2017-06-27 11:02:09

标签: soapui jsonpath

我使用SoapUi 5.3.0来测试REST API。

我的请求如下所示返回Json:

[
   {
      "name": "John",
      "online_status": "online"
   },
   {
      "name": "Marry",
      "online_status": "online"
   }
]

如何使用JsonPath expresstion验证所有" online_status"在线,而不是其他

1 个答案:

答案 0 :(得分:0)

您可以使用Script Assertion进行相同的REST请求测试步骤。

你走了:

assert context.response, 'Response is null or empty'
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Get the online_status as Set; so unique value are stored
def result = json.collect{it.'online_status'} as Set
//It should be only one as expected
assert 1 == result.size()
//It should only have online
assert result == ['online'] as Set

如果您想快速尝试脚本,请检查 Demo