添加断言以验证结构

时间:2017-02-27 20:55:48

标签: json rest groovy soapui assertions

我正在使用soapUI免费版,并想添加两个断言

  • 检查密钥的第一个断言始终存在于响应正文消息中
  • 每个键值对的验证值都是正确的

例如:

{
      "data": "",
      "success": ""
      "statuscode": ""
}

任何人都可以指出我们如何使用soapUI免费版本实现这一目标。 groovy脚本只能实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Script Assertion作为测试用例中的请求步骤来实现您提到的断言。

例如,如果您采用您提到的示例数据:

脚本断言

//Below is the key value pair map that you are expecting from response
//So define according to the expaction. Now just showing with example
//values for demonstration
def expectedMap = [data :'', success :'', statuscode :'']

def response = """{
  "data": "",
  "success": ""
  "statuscode": ""
}"""

def json = new groovy.json.JsonSlurper().parseText(response)
//check if actual response's keys and values are matched with expected map
//Both of your questions are fulfilled by this
assert expectedMap == json, 'Both are not matching'

您可以在线快速尝试 Demo

<强>输出:
enter image description here

如果你想处理动态响应(与上面的固定响应不同),那么你可以使用下面的脚本而不是上面的脚本。

def expectedMap = [data :'', success :'', statuscode :'']

assert context.response, 'Response received is either empty or null'

def json = new groovy.json.JsonSlurper().parseText(context.response)

assert expectedMap == json, 'Actual response is not matching with expected data'