Json断言-错误断言失败消息:预期值与正则表达式匹配

时间:2019-09-04 06:51:28

标签: json jmeter

使用Jmeter执行REST API测试。当我在HTTP响应中使用JSON Path Tester并将JSON路径表达式提供为$..name时。获得以下比赛

Done,
Won't Do,
Duplicate,
Cannot Reproduce

enter image description here

但是当我在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"]'

enter image description here

2 个答案:

答案 0 :(得分:0)

匹配多个值

  1. 使用JSON提取器从JSON响应中提取所有匹配的值
  2. 通过以下代码使用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);
    }
    

enter image description here

匹配单个值

  1. 使用JSON提取器提取值
  2. 使用JSON断言来声明值。在Expected Value部分中输入任何单个值:"Done/Won't Do/Duplicate/Cannot Reproduce"

enter image description here

答案 1 :(得分:0)

如果这确实是您要实现的目标,请考虑以下JSON Assertion配置:

enter image description here

仅在“期望值”文本表示为假的情况下:

["Done","Won't Do","Duplicate","Cannot Reproduce"]

无论如何,这种形式的断言将是脆弱的,并且对任何JSON更改都是敏感的,即,如果匹配顺序不同,则断言将失败。因此,我宁愿建议使用The Easiest Way To Compare REST API Responses Using JMeter文章中描述的方法之一。