postman之如何实现接⼝测试断⾔json返回值(多层级
json)+判断返回值中包含字符
本节⽬的:使⽤实现接⼝断⾔,取接⼝中的某个字段验证
⽰例接⼝:
备注:接⼝为Get类型(该接⼝为某应⽤搜索接⼝,搜索内容为“号外天下”)
步骤如下:
1. 双击启动postman
2. 请求接⼝
在右侧输⼊接⼝地址如下图,⼤家可以直接点击nd看看该接⼝返回的是什么值
实际接⼝返回如下:
1{
2 "ur_list": [
3 {
4 "avatar": "/2016/11/16/1479280668359254.jpg",
粗犷造句5 "intro": "",
6 "new_source": "1",
7 "nickname": "号外天下",
班组安全
8 "nickname_highlighted": "<font color=\"#FF0000\">号外天下</font>",
9 "weixin": "hwtx"
10 }
11 ],
12 "result": {
12 "result": {
13 "code": "1",
14 "message": "执⾏成功"
15 },
16 "article_list": [
17 {
18 "aid": "0KDE3WtYbfPm3aO28cHhTkg==",
19 "flag": "0",
20 "imgurl": "/2018/01/23/bbcd0f0335f0eefb221b7fa88204e57e.jpeg",
21 "new_source": "1",
22 "nickname": "⼤风车艺术教育",
23 "readNum": "3586",
24 "title": "【号外号外】⼉童励志剧《少年志》",
25 "title_highlighted": "【<font color=\"#FF0000\">号外号外</font>】⼉童励志剧《少年志》",
26 "url": "/api/article/get_template?template=content&aid=0KDE3WtYbfPm3aO28cHhTkg==&newSource=1",
27 "videoCovers": "",
组合式衣柜28 "videourls": "",
29 "weixin": "dfc88190139",
30 "weixinurl": "/api/weixin/get_info?weixin=dfc88190139&newSource=1"
31 },
32 {
33 "aid": "KB0N8JT5z9rm3aO28cHhTkg==",
34 "flag": "0",
35 "imgurl": "/2018/01/12/d662da4951c40465931ab96c9acb99d6.jpeg",
36 "new_source": "1",
37 "nickname": "百⽶⽣活唐⼭",
38 "readNum": "1230",
39 "title": "【号外!号外!】唐⼭“三⾼”企业招⼈啦!",
40 "title_highlighted": "【<font color=\"#FF0000\">号外</font>!<font color=\"#FF0000\">号外</font>!】唐⼭“三⾼”企业招⼈啦!",
41 "url": "/api/article/get_template?template=content&aid=KB0N8JT5z9rm3aO28cHhTkg==&newSource=1",
42 "videoCovers": "",
43 "videourls": "",
44 "weixin": "bmshts",
45 "weixinurl": "/api/weixin/get_info?weixin=bmshts&newSource=1"
46 },
47 {
48 "aid": "hdoTEoPm1HfHm3aO28cHhTkg==",
49 "flag": "0",
50 "imgurl": "/2018/03/24/c2d3f13f8824e11b06e47e1f176f3e54.jpeg",
51 "new_source": "1",
52 "nickname": "全球设计",
53 "readNum": "1817",
54 "title": "号外号外,新模式!让装修像购物⼀样简单!",
55 "title_highlighted": "<font color=\"#FF0000\">号外号外</font>,新模式!让装修像购物⼀样简单!",
体落56 "url": "/api/article/get_template?template=content&aid=hdoTEoPm1HfHm3aO28cHhTkg==&newSource=1",
57 "videoCovers": "",
58 "videourls": "",
59 "weixin": "LuxeDesign",
60 "weixinurl": "/api/weixin/get_info?weixin=LuxeDesign&newSource=1"
61 },
62 {
63 "aid": "wl6hJJiQyF7m3aO28cHhTkg==",
64 "flag": "0",
65 "imgurl": "/2018/03/03/899752404aa2ab8708076d03c256f6e6.jpeg",
66 "new_source": "1",
67 "nickname": "漳州头条",
68 "readNum": "905",
69 "title": "号外!号外!这件事和你们家的宝宝有关!",
70 "title_highlighted": "<font color=\"#FF0000\">号外</font>!<font color=\"#FF0000\">号外</font>!这件事和你们家的宝宝有关!",
71 "url": "/api/article/get_template?template=content&aid=wl6hJJiQyF7m3aO28cHhTkg==&newSource=1",
72 "videoCovers": "",
73 "videourls": "",
74 "weixin": "zzshwx",
75 "weixinurl": "/api/weixin/get_info?weixin=zzshwx&newSource=1"
76 },
77 {
77 {
78 "aid": "aSI06wZOW63m3aO28cHhTkg==",
79 "flag": "0",
80 "imgurl": "/2018/02/28/29c279f28bef6e6f80edc933ee4e3312.jpeg",
81 "new_source": "1",
82 "nickname": "ZERO动漫⽹",
83 "readNum": "6389",
84 "title": "藏尽天下",
85 "title_highlighted": "藏尽<font color=\"#FF0000\">天下</font>",
86 "url": "/api/article/get_template?template=content&aid=aSI06wZOW63m3aO28cHhTkg==&newSource=1",
87 "videoCovers": "",
88 "videourls": "",
89 "weixin": "ZERODMW",
90 "weixinurl": "/api/weixin/get_info?weixin=ZERODMW&newSource=1"
91 },
92 {
93 "aid": "d2rm3BDtW3pLm3aO28cHhTkg==",
94 "flag": "0",
95 "imgurl": "/2018/02/09/cf8cd058d185117495b44ebc22370127.jpeg",
96 "new_source": "1",
97 "nickname": "抚州公安",
98 "readNum": "699",
99 "title": "号外~号外~| 公安局会给这种⾏为发邀请函哦!",
100 "title_highlighted": "<font color=\"#FF0000\">号外</font>~<font color=\"#FF0000\">号外</font>~| 公安局会给这种⾏为发邀请函哦!", 101 "url": "/api/article/get_template?template=content&aid=d2rm3BDtW3pLm3aO28cHhTkg==&newSource=1",
102 "videoCovers": "",
103 "videourls": "",
104 "weixin": "fzgaplice",
105 "weixinurl": "/api/weixin/get_info?weixin=fzgaplice&newSource=1"
106 },
107 {
108 "aid": "xrxVm3ET4Uhfm3aO28cHhTkg==",
109 "flag": "0",
110 "imgurl": "/2018/02/03/35b3fc35a43a15b19cb982f86d78399f.jpeg",
111 "new_source": "1",
112 "nickname": "博州⼥声",
113 "readNum": "752",
114 "title": "博州 |号外号外,博州妇联荣获全国⼤奖啦",
115 "title_highlighted": "博州 |<font color=\"#FF0000\">号外号外</font>,博州妇联荣获全国⼤奖啦",
116 "url": "/api/article/get_template?template=content&aid=xrxVm3ET4Uhfm3aO28cHhTkg==&newSource=1",
117 "videoCovers": "",
118 "videourls": "",
119 "weixin": "gh_05bd55d77f96",
120 "weixinurl": "/api/weixin/get_info?weixin=gh_05bd55d77f96&newSource=1"
121 },
122 {
123 "aid": "dPg2rHm3U5Lfm3aO28cHhTkg==",
124 "flag": "0",
125 "imgurl": "/2018/01/31/d268f2c4a790832a0db9ec69d83136c9.jpeg",
中药牛大力126 "new_source": "1",
127 "nickname": "莱芜移动",
128 "readNum": "1212",
129 "title": "【号外号外】快来领新春60元话费啦~",
130 "title_highlighted": "【<font color=\"#FF0000\">号外号外</font>】快来领新春60元话费啦~",
131 "url": "/api/article/get_template?template=content&aid=dPg2rHm3U5Lfm3aO28cHhTkg==&newSource=1",
132 "videoCovers": "",
133 "videourls": "",
134 "weixin": "LW-CMCC",
135 "weixinurl": "/api/weixin/get_info?weixin=LW-CMCC&newSource=1"
136 },
137 {
138 "aid": "wZ2HT7F6JhXm3aO28cHhTkg==",
139 "flag": "0",
140 "imgurl": "/2018/01/22/d6dc9bbeb0544c664d56780a11022bcb.jpeg",
141 "new_source": "1",
142 "nickname": "⼤庆⾼新区法院",
3. 设置断⾔
高二物理知识点
我们不可能写好接⼝之后,每次挨个点击进去查看⼀下是否返回数据正确,因此需要我们设置⼀些判断,判断这个接⼝是否正常运⾏。这个
判断就叫断⾔。postman中的断⾔是在tests中设置,如下图(点击test即进⼊了断⾔设置区)
⼀、断⾔返回的json数据中是否存在“result“字段
1. 点击上图中右⽅红⾊字体“Respon body:contains string"
2. 点击之后,会在tests框中⾃动⽣成⼀串代码如下:
142
"nickname": "⼤庆⾼新区法院",143
"readNum": "1192",144
"title": "号外!号外!驻院律师缘何不收费?",145
"title_highlighted": "<font color=\"#FF0000\">号外</font>!<font color=\"#FF0000\">号外</font>!驻院律师缘何不收费?",146
"url": "/api/article/get_template?template=content&aid=wZ2HT7F6JhXm3aO28cHhTkg==&newSource=1",147
"videoCovers": "",148
"videourls": "",149
"weixin": "dagxqfy",150
"weixinurl": "/api/weixin/get_info?weixin=dagxqfy&newSource=1"151
},152
{153
"aid": "3XREJce25Fnm3aO28cHhTkg==",154
"flag": "0",155
设置行高"imgurl": "/2018/01/23/9c9fab4300c9301ff171a2ca9129412b.jpeg",156
"new_source": "1",157
"nickname": "合肥⽓象",158
"readNum": "1102",159
"title": "号外!号外!!暴雪已发货,记得24⽇晚签收哦~",160
"title_highlighted": "<font color=\"#FF0000\">号外</font>!<font color=\"#FF0000\">号外</font>!!暴雪已发货,记得24⽇晚签收哦~",161
"url": "/api/article/get_template?template=content&aid=3XREJce25Fnm3aO28cHhTkg==&newSource=1",162
"videoCovers": "",163
"videourls": "",164
"weixin": "hf_goodweather",165
"weixinurl": "/api/weixin/get_info?weixin=hf_goodweather&newSource=1"166
黄芩的功效与作用点}167 ]168}
3. 修改测试命名以及检测测result字段是否存在
4. 点击nd运⾏接⼝,结果如下:
5.
1.
6. 上图body为接⼝请求返回的参数,点击test results可以查看断⾔结果,红⾊为失败,蓝⾊为断⾔正确⼆、断⾔json返回值中的ur_list下第⼀个结果的nickname值为”号外天下“
1. 点击右⽅红⾊字体:Respon body:Json value check
2. 将系统⽣成的代码修改为:
三、断⾔断⾔json返回值中的article_list下第⼆个结果的nickname值为”百⽶⽣活唐⼭“
tests["Body matches string"] = responBody.has("string_you_want_to_arch");
tests["检测是否返回result 字段"] = responBody.has("result");
1var jsonData = JSON.par(responBody);2tests["检查公众名称"] = jsonData.ur_list[0].nickname === "号外天下"
tests["检查⽂章名称"] = jsonData.article_list[1].nickname === "百⽶⽣活唐⼭"