使用Jmeter执行REST API测试。当我在HTTP响应中使用JSON Path Tester并将JSON路径表达式提供为$..name
时。获得以下比赛
Done,
Won't Do,
Duplicate,
Cannot Reproduce
但是当我在JSON断言中放置相同的JSON表达式和Result时,出现错误:-
Assertion error: false
Assertion failure: true
Assertion failure message: Value expected to match regexp '"Done","Won't Do","Duplicate","Cannot Reproduce"', but it did not match: '["Done","Won't Do","Duplicate","Cannot Reproduce"]'
答案 0 :(得分:0)
匹配多个值
通过以下代码使用JSR223声明:
int nameLength = Integer.parseInt(vars.get("Name_matchNr"));
String[] responseArray = new String[nameLength];
for (int i = 0; i < nameLength; i++) {
responseArray[i] = vars.get("Name_"+(i+1));
}
boolean test1 = Arrays.asList(responseArray).contains("Done");
boolean test2 = Arrays.asList(responseArray).contains("Won't Do");
boolean test3 = Arrays.asList(responseArray).contains("Duplicate");
boolean test4 = Arrays.asList(responseArray).contains("Cannot Reproduce");
if (test1 == false) {
AssertionResult.setFailureMessage("Done not present in Response");
AssertionResult.setFailure(true);
}
if (test2 == false) {
AssertionResult.setFailureMessage("Won't Do not present in Response");
AssertionResult.setFailure(true);
}
if (test3 == false) {
AssertionResult.setFailureMessage("Duplicate not present in Response");
AssertionResult.setFailure(true);
}
if (test4 == false) {
AssertionResult.setFailureMessage("Cannot Reproduce not present in Response");
AssertionResult.setFailure(true);
}
匹配单个值
Expected Value
部分中输入任何单个值:"Done/Won't Do/Duplicate/Cannot Reproduce"
答案 1 :(得分:0)
如果这确实是您要实现的目标,请考虑以下JSON Assertion配置:
仅在“期望值”文本表示为假的情况下:
["Done","Won't Do","Duplicate","Cannot Reproduce"]
无论如何,这种形式的断言将是脆弱的,并且对任何JSON更改都是敏感的,即,如果匹配顺序不同,则断言将失败。因此,我宁愿建议使用The Easiest Way To Compare REST API Responses Using JMeter文章中描述的方法之一。