用jmeter验证精确的json

时间:2016-11-08 14:59:31

标签: json jmeter

使用JMeter进行REST服务测试:我有确切的JSON是我的预期结果,请帮我在JMeter中定义这个断言。例如,我对GET请求的预期响应恰好是:

[ {
  "id" : "FR",
  "description" : "France",
  "defaultLanguageId" : "fr",
  "defaultTimezone" : "Europe/Paris"
}, {
  "id" : "IT",
  "description" : "Italy",
  "defaultLanguageId" : "it",
  "defaultTimezone" : "Europe/Rome"
} ]

1 个答案:

答案 0 :(得分:1)

我会选择JSR223 Assertion和Groovy语言:

  1. 将JSR223断言添加为返回上述JSON
  2. 的请求的子节点
  3. 将以下代码放入JSR223 Assertion" Script"面积:

    import groovy.json.JsonSlurper
    
    JsonSlurper slurper = new JsonSlurper();
    
    def expected = slurper.parseText("[\n" +
                    "  {\n" +
                    "    \"id\": \"FR\",\n" +
                    "    \"description\": \"France\",\n" +
                    "    \"defaultLanguageId\": \"fr\",\n" +
                    "    \"defaultTimezone\": \"Europe/Paris\"\n" +
                    "  },\n" +
                    "  {\n" +
                    "    \"id\": \"IT\",\n" +
                    "    \"description\": \"Italy\",\n" +
                    "    \"defaultLanguageId\": \"it\",\n" +
                    "    \"defaultTimezone\": \"Europe/Rome\"\n" +
                    "  }\n" +
                    "]");
    def actual = slurper.parseText(prev.getResponseDataAsString());
    
    if (expected != actual) {
        SampleResult.setSuccessful(false);
    }
    
  4. 使用这种方法,您将能够比较JSON结构,并且在不同节点顺序或额外空间或其他情况下它不会失败。如果您还需要检查订单和标记 - 只需在Equals模式下使用普通Response Assertion,例如:

    Response Assertion

    参考文献: