使用JSON提取器,我设法获取了以下需要从整个JSON响应中取出的数据。现在,我需要验证每个字段的值范围,例如。年鉴应在例如之间。每年1-10升,年龄18-30岁,教育程度可为B.A,B.Sc。或B.Com
{
: "ID":"M12345",
: "EDUCATION":"B.A.",
: "ANNUALINCOME":"5 - 6 L\/annum",
: "AGE":"29"
}
mid_10=
{
: "ID":"M12346",
: "EDUCATION":"B.Sc.",
: "ANNUALINCOME":"1 - 2 L\/annum",
: "AGE":"24"
}
mid_11=
{
: "ID":"M12347",
: "EDUCATION":"B.Com.",
: "ANNUALINCOME":"5 - 6 L\/annum",
: "AGE":"27"
}
我应该在这里使用Response Assertion吗?我还需要检查传递了哪些ID,哪些失败了。理想情况下,即使我没有获得通过的ID,我也应该获得失败的ID。
预先感谢您的帮助。
答案 0 :(得分:1)
您的接受标准过于具体,因此您将无法使用响应声明。
您将必须使用JSR223 Assertion和Groovy scripting,断言代码示例如下:
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def income = response.ANNUALINCOME
def lowerBound = (income =~ "(\\d+)")[0][1] as int
def upperBound = (income =~ "(\\d+)")[1][1] as int
def average = (lowerBound + upperBound) / 2
def acceptableIncomeRange = 1..10
if (!acceptableIncomeRange.contains(average)) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Annual income is not within the acceptable range: ' + income)
}
def acceptableAgeRange = 18..30
def age = response.AGE as int
if (!acceptableAgeRange.contains(age)) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Age is not within the acceptable range: ' + age)
}
def acceptableEducations = ['B.A.', 'B.Sc.', 'B.Com']
def education = response.EDUCATION
if (!acceptableEducations.contains(education)) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Education is not within the acceptable range: ' + education)
}
参考文献: