postman之如何实现接口测试断言json返回值(多层级json) +判断返回值中包含字符

本节目的:使用postman实现接口断言,取接口中的某个字段验证

示例接口:

http://api.myhaowai.com/api/search?devid=D9876540D550EF42AB10C0F3AABB6305&page=1&pcode=021000&query=%E5%8F%B7%E5%A4%96%E5%A4%A9%E4%B8%8B&version=3.4

备注:接口为Get类型(该接口为某应用搜索接口,搜索内容为“号外天下”)

 

步骤如下:

1. 双击启动postman

2. 请求接口

在右侧输入接口地址如下图,大家可以直接点击send看看该接口返回的是什么值

postman之如何实现接口测试断言json返回值(多层级json) +判断返回值中包含字符_第1张图片

 

 

 

实际接口返回如下:

{
    "user_list": [
        {
            "avatar": "https://img2.myhaowai.cn/2016/11/16/1479280668359254.jpg",
            "intro": "",
            "new_source": "1",
            "nickname": "号外天下",
            "nickname_highlighted": "号外天下",
            "weixin": "hwtx"
        }
    ],
    "result": {
        "code": "1",
        "message": "执行成功"
    },
    "article_list": [
        {
            "aid": "0KDE3WtYbfPm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img1.myhaowai.cn/2018/01/23/bbcd0f0335f0eefb221b7fa88204e57e.jpeg",
            "new_source": "1",
            "nickname": "大风车艺术教育",
            "readNum": "3586",
            "title": "【号外号外】儿童励志剧《少年志》",
            "title_highlighted": "【号外号外】儿童励志剧《少年志》",
            "url": "/api/article/get_template?template=content&aid=0KDE3WtYbfPm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "dfc88190139",
            "weixinurl": "/api/weixin/get_info?weixin=dfc88190139&newSource=1"
        },
        {
            "aid": "KB0N8JT5z9rm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img2.myhaowai.cn/2018/01/12/d662da4951c40465931ab96c9acb99d6.jpeg",
            "new_source": "1",
            "nickname": "百米生活唐山",
            "readNum": "1230",
            "title": "【号外!号外!】唐山“三高”企业招人啦!",
            "title_highlighted": "【号外号外!】唐山“三高”企业招人啦!",
            "url": "/api/article/get_template?template=content&aid=KB0N8JT5z9rm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "bmshts",
            "weixinurl": "/api/weixin/get_info?weixin=bmshts&newSource=1"
        },
        {
            "aid": "hdoTEoPm1HfHm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img2.myhaowai.cn/2018/03/24/c2d3f13f8824e11b06e47e1f176f3e54.jpeg",
            "new_source": "1",
            "nickname": "全球设计",
            "readNum": "1817",
            "title": "号外号外,新模式!让装修像购物一样简单!",
            "title_highlighted": "号外号外,新模式!让装修像购物一样简单!",
            "url": "/api/article/get_template?template=content&aid=hdoTEoPm1HfHm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "LuxeDesign",
            "weixinurl": "/api/weixin/get_info?weixin=LuxeDesign&newSource=1"
        },
        {
            "aid": "wl6hJJiQyF7m3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img0.myhaowai.cn/2018/03/03/899752404aa2ab8708076d03c256f6e6.jpeg",
            "new_source": "1",
            "nickname": "漳州头条",
            "readNum": "905",
            "title": "号外!号外!这件事和你们家的宝宝有关!",
            "title_highlighted": "号外号外!这件事和你们家的宝宝有关!",
            "url": "/api/article/get_template?template=content&aid=wl6hJJiQyF7m3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "zzshwx",
            "weixinurl": "/api/weixin/get_info?weixin=zzshwx&newSource=1"
        },
        {
            "aid": "aSI06wZOW63m3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img0.myhaowai.cn/2018/02/28/29c279f28bef6e6f80edc933ee4e3312.jpeg",
            "new_source": "1",
            "nickname": "ZERO动漫网",
            "readNum": "6389",
            "title": "藏尽天下",
            "title_highlighted": "藏尽天下",
            "url": "/api/article/get_template?template=content&aid=aSI06wZOW63m3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "ZERODMW",
            "weixinurl": "/api/weixin/get_info?weixin=ZERODMW&newSource=1"
        },
        {
            "aid": "d2rm3BDtW3pLm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img0.myhaowai.cn/2018/02/09/cf8cd058d185117495b44ebc22370127.jpeg",
            "new_source": "1",
            "nickname": "抚州公安",
            "readNum": "699",
            "title": "号外~号外~| 公安局会给这种行为发邀请函哦!",
            "title_highlighted": "号外~号外~| 公安局会给这种行为发邀请函哦!",
            "url": "/api/article/get_template?template=content&aid=d2rm3BDtW3pLm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "fzgaplice",
            "weixinurl": "/api/weixin/get_info?weixin=fzgaplice&newSource=1"
        },
        {
            "aid": "xrxVm3ET4Uhfm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img3.myhaowai.cn/2018/02/03/35b3fc35a43a15b19cb982f86d78399f.jpeg",
            "new_source": "1",
            "nickname": "博州女声",
            "readNum": "752",
            "title": "博州 |号外号外,博州妇联荣获全国大奖啦",
            "title_highlighted": "博州 |号外号外,博州妇联荣获全国大奖啦",
            "url": "/api/article/get_template?template=content&aid=xrxVm3ET4Uhfm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "gh_05bd55d77f96",
            "weixinurl": "/api/weixin/get_info?weixin=gh_05bd55d77f96&newSource=1"
        },
        {
            "aid": "dPg2rHm3U5Lfm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img2.myhaowai.cn/2018/01/31/d268f2c4a790832a0db9ec69d83136c9.jpeg",
            "new_source": "1",
            "nickname": "莱芜移动",
            "readNum": "1212",
            "title": "【号外号外】快来领新春60元话费啦~",
            "title_highlighted": "【号外号外】快来领新春60元话费啦~",
            "url": "/api/article/get_template?template=content&aid=dPg2rHm3U5Lfm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "LW-CMCC",
            "weixinurl": "/api/weixin/get_info?weixin=LW-CMCC&newSource=1"
        },
        {
            "aid": "wZ2HT7F6JhXm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img3.myhaowai.cn/2018/01/22/d6dc9bbeb0544c664d56780a11022bcb.jpeg",
            "new_source": "1",
            "nickname": "大庆高新区法院",
            "readNum": "1192",
            "title": "号外!号外!驻院律师缘何不收费?",
            "title_highlighted": "号外号外!驻院律师缘何不收费?",
            "url": "/api/article/get_template?template=content&aid=wZ2HT7F6JhXm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "dagxqfy",
            "weixinurl": "/api/weixin/get_info?weixin=dagxqfy&newSource=1"
        },
        {
            "aid": "3XREJce25Fnm3aO28cHhTkg==",
            "flag": "0",
            "imgurl": "https://img4.myhaowai.cn/2018/01/23/9c9fab4300c9301ff171a2ca9129412b.jpeg",
            "new_source": "1",
            "nickname": "合肥气象",
            "readNum": "1102",
            "title": "号外!号外!!暴雪已发货,记得24日晚签收哦~",
            "title_highlighted": "号外号外!!暴雪已发货,记得24日晚签收哦~",
            "url": "/api/article/get_template?template=content&aid=3XREJce25Fnm3aO28cHhTkg==&newSource=1",
            "videoCovers": "",
            "videourls": "",
            "weixin": "hf_goodweather",
            "weixinurl": "/api/weixin/get_info?weixin=hf_goodweather&newSource=1"
        }
    ]
}

3. 设置断言

我们不可能写好接口之后,每次挨个点击进去查看一下是否返回数据正确,因此需要我们设置一些判断,判断这个接口是否正常运行。这个判断就叫断言。postman中的断言是在tests中设置,如下图(点击test即进入了断言设置区)

postman之如何实现接口测试断言json返回值(多层级json) +判断返回值中包含字符_第2张图片

一、断言返回的json数据中是否存在“result“字段

 

  1. 点击上图中右方红色字体“Response body:contains string"
  2. 点击之后,会在tests框中自动生成一串代码如下:
    tests["Body matches string"] = responseBody.has("string_you_want_to_search");
  3. 修改测试命名以及检测测result字段是否存在
    tests["检测是否返回result字段"] = responseBody.has("result");
  4. 点击send运行接口,结果如下:
    1. postman之如何实现接口测试断言json返回值(多层级json) +判断返回值中包含字符_第3张图片
  5. 上图body为接口请求返回的参数,点击test results可以查看断言结果,红色为失败,蓝色为断言正确

二、断言json返回值中的user_list下第一个结果的nickname值为”号外天下“

 

  1.  点击右方红色字体:Response body:Json value check
  2. 将系统生成的代码修改为:
    var jsonData = JSON.parse(responseBody);
    tests["检查公众名称"] = jsonData.user_list[0].nickname === "号外天下"

三、断言断言json返回值中的article_list下第二个结果的nickname值为”百米生活唐山“

tests["检查文章名称"] = jsonData.article_list[1].nickname === "百米生活唐山"

 

你可能感兴趣的:(接口测试相关)