欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

postman高级用法--断言(Response body:JSON value check)

程序员文章站 2022-03-06 18:02:22
...

今天在小伙伴的分享中遇到了个问题,postman中的高级用法--断言,因为我们返回的json千奇百怪,所以呢,需要我们按照js的用法,来找出具体到json最内层的字段。

废话不说了,直接搞:

我的接口返回的json长成这个鬼样子。

{
    "code": 0,
    "message": "success",
    "data": {
        "goods_list": [
            {
                "goods_id": "JD_60436279224",
                "goods_source": "web",
                "goods_url": "https://homed-inhouse.bytedance.net/growth/product-details/?uniq_pid=JD_60436279224&invoke_source=0",
                "goods_avatar": "https://img14.360buyimg.com/img/jfs/t1/81450/7/14123/748861/5db82709Ee9b8658b/b5b2a53c1c8ee15d.jpg",
                "goods_title": "一次性浴帽防水防油烟洗澡淋浴帽子女款头套沐浴冒厨房发帽 白色散装1包(约100只) 加厚",
                "goods_price": "¥15.7"
            },
            {
                "goods_id": "JD_2496994",
                "goods_source": "web",
                "goods_url": "https://homed-inhouse.bytedance.net/growth/product-details/?uniq_pid=JD_2496994&invoke_source=0",
                "goods_avatar": "https://img11.360buyimg.com/img/jfs/t3226/21/4458434382/198206/4c02ab04/5849247aN91f6939b.jpg",
                "goods_title": "NATURAL COLOR 鱼缸 造景石 鱼缸装饰 底砂 水族箱 造景 用品造景蓝色 磨砂海蓝腰果500g(约60~70颗)",
                "goods_price": "¥18"
            },
            {
                "goods_id": "JD_56799618502",
                "goods_source": "web",
                "goods_url": "https://homed-inhouse.bytedance.net/growth/product-details/?uniq_pid=JD_56799618502&invoke_source=0",
                "goods_avatar": "https://img12.360buyimg.com/img/jfs/t1/76992/29/10440/192025/5d7b3cc2E8728a397/201b2f7ad2dcbfb4.jpg",
                "goods_title": "【京东新品】数字油画油彩画动漫风景人物填色画手工数字画填充压装饰画B01 A558 40*50彩色画板(厚框)  画板+颜料+画笔+无",
                "goods_price": "¥102"
            },
            {
                "goods_id": "JD_51542690471",
                "goods_source": "web",
                "goods_url": "https://homed-inhouse.bytedance.net/growth/product-details/?uniq_pid=JD_51542690471&invoke_source=0",
                "goods_avatar": "https://img13.360buyimg.com/img/jfs/t1/46814/4/4055/290947/5d1c63c9E8aee4423/612a4da6e62a1727.jpg",
                "goods_title": "简微娜 床笠单件棉全棉加厚席梦思床垫保护套加密裸睡1.5 1.8m床套床罩 羽彩纷飞-豆沙 120cmx200cm(加厚款升级)",
                "goods_price": "¥138"
            }
        ],
        "recommend_rectangle": null,
        "request_rectangle": {
            "left_top_x_percent": 0.41,
            "left_top_y_percent": 0.41,
            "right_bottom_x_percent": 0.59,
            "right_bottom_y_percent": 0.59
        },
        "log_id": "201912261839170100170221671000621A"
    }
}

那么postman上面如何搞呢。

postman高级用法--断言(Response body:JSON value check)

下面我想取出json中最内层的goods_id进行断言

1、取到data

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data).to.eql("字符串");
});

2、取到上面json的goods_list

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.goods_list).to.eql("字符串");
});

3、取到上面json的第一个goods_id

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.goods_list[0].goods_id).to.eql("JD_60436279224");
});

然后我就可以成功了。。。

postman高级用法--断言(Response body:JSON value check)

相关标签: 测试