Postman断言:Response body :Json value check(响应体检测某个json的值)

获取返回的Json中内层的数据

1、Response body :Json value check代码格式如下:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});

“Your test name”可自定义

pm.response.json()响应体转换为json,并存储在jsonData中

jsonData.value获取json内容,比较字段的值

2、实例如下所示:

{
    "status": 0,
    "result": {
        "location": {
            "areacode": "101010100",
            "name": "北京",
            "country": "中国",
            "path": "北京,北京市,北京市,中国"
        },
        "realtime": {
            "text": "阴",
            "code": "02",
            "temp": 17.2,
            "feels_like": 15,
            "rh": 12,
            "wind_class": "2级",
            "wind_speed": 2.9,
            "wind_dir": "东北风",
            "wind_angle": 63,
            "prec": 0.0,
            "clouds": 95,
            "vis": 30000,
            "pressure": 1012,
            "dew": -12,
            "uv": 1,
            "weight": 7,
            "brief": "凉而干燥",
            "detail": "天气干燥,注意肌肤保湿哦"
        },
        "last_update": "2023-04-20 17:16"
    }
}

3、假如获取“areacode”的值进行断言,格式如下

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.result.location.areacode).to.eql("101010100");
});

4、结果如下

Postman断言:Response body :Json value check(响应体检测某个json的值)_第1张图片

 

你可能感兴趣的:(postman,测试工具,学习)