在JSON Response中为指定的值范围验证多个参数

问题描述 投票:0回答:1

使用JSON Extractor我设法获得了以下需要从整个JSON响应中获取的数据。现在,我需要为每个字段验证一系列值,例如。年龄应该在例如之间。 1 - 10 L /年,年龄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,即使我没有得到通过的ID。

在此先感谢您的帮助。

json jmeter assert jsonresponse
1个回答
1
投票

您的验收标准过于具体,因此您将无法使用响应声明。

你将不得不去JSR223 AssertionGroovy 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)
}

参考文献:

© www.soinside.com 2019 - 2024. All rights reserved.