针对指定的值范围验证JSON响应中的多个参数
使用JSON提取器,我成功地获取了以下数据片段,我需要从整个JSON响应中获取这些数据。现在,我需要验证每个字段的值范围,例如,年收入应该在1-10升/年之间,年龄18-30岁,教育程度可以是学士、理学学士。或者B.Com针对指定的值范围验证JSON响应中的多个参数,json,jmeter,assert,jsonresponse,Json,Jmeter,Assert,Jsonresponse,使用JSON提取器,我成功地获取了以下数据片段,我需要从整个JSON响应中获取这些数据。现在,我需要验证每个字段的值范围,例如,年收入应该在1-10升/年之间,年龄18-30岁,教育程度可以是学士、理学学士。或者B.Com { : "ID":"M12345", : "EDUCATION":"B.A.", : "ANNUALINCOME":"5 - 6 L\/annum", : "AGE":"29" } mid_10= { : "ID":"M12346", : "EDUC
{
: "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"
}
我应该在这里使用响应断言吗?我还需要检查哪些ID通过,哪些ID失败。理想情况下,我应该获得失败的ID,即使我没有获得通过的ID
提前感谢您的帮助。您的接受标准太具体,因此您将无法使用响应断言 您将不得不使用以下示例断言代码:
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)
}
参考资料:
ANNUALINCOME
值不匹配,除非看到有效的JSON,不是你说的那些废话