我有以下创建员工的请求
{
"name": "abc",
"dept": "mech",
"dob": "20-Feb-1994"
}
我得到的回复相同,另外还有员工的身份证明。 我正在写声明回应所有属性,如
"name":"${CreateEmp#Request#$.name}"
有没有简单的方法来测试以下条件?
来自评论的编辑:
{
"empid":"4187",
"deptId":"4",
"branchId":"6",
"firstName":"Prabhu",
"lastName":"S",
"emailAddress":"prabhus@gmail.com",
"contactNumber":"987654321",
"isEnabled":"Y",
"dob":"02/05/1994",
"doj":"03/04/2016",
"fatherName":"XXXX",
"motherName":"YYYY",
"activationCode":"ssp",
"spouseName":"ZZZZ",
"address":"MMMMMM",
"pincode":"123456",
"district":"16",
"dateCreated":"2017-02-21T13:00:24.317Z",
"dateModified":"2017-02-21T13:00:24.317Z",
"status":"0"
}
答案 0 :(得分:2)
这是一个groovy脚本,它根据请求值进行响应比较
Groovy脚本:将此作为Script Assertion
用于请求步骤,以便它可以动态地处理任何请求和响应,而不是固定的json。
//Provide the keys to be ignored
def ignoreKeys = ['empid', 'dateCreated', 'dateModified']
def jsonRequest = context.rawRequest
def jsonResponse = context.response
def reqParsed = new groovy.json.JsonSlurper().parseText(jsonRequest)
def resParsed = new groovy.json.JsonSlurper().parseText(jsonResponse)
def sb = new StringBuffer()
reqParsed.keySet().each { key ->
if (!ignoreKeys.contains(key)) {
reqParsed."$key" == resParsed."$key" ?: sb.append("${key} value does not match; request[${reqParsed."$key"}], response[${resParsed."$key"}]\n")
}
}
if(sb.toString()) {
throw new Error("There are differences between the request values and response values. Details below:\n${sb.toString()}")
}
在这里,您可以使用您提供的固定样本快速在线 Demo 。
输出已使用不同的值来声明断言作品