postman 接口参数化操作

最近一直忙于AI模型的准确率任务中,这种操作需要大量的数据才能计算出模型的准确率。所以这里问遇到的问题和之前数据随机参数化有点不同,之前的参数比如说用户姓名或用户身份证号,这样数据可以在postman中直接用代码随机生成,可以参考postman接口测试系列:基本操作总结 ,但是此处采用的是本地文件参数化,和jmeter的csv参数化比较类似。

先来看一下单个食物识别的接口信息:

https://api.ty.com/food_detect

而参数主要是

msg:西红柿鸡蛋面

响应结果如下:

[ { "properties": { "cal_name": "鸡", "e_type": "food", "end_index": 1, "food_id": 6014, "kcal_unit_weight": 167.0, "name": "鸡", "start_index": 0, "time_stamp": 1567236416 }, "sub_properties": { "grams": 150.0, "is_default": true, "quantifier_id": 35, "quantity": 1.0, "unit": "盘" }, "type": "Entity" } ]

这里断言的主要是响应结果中的cal_name和name名称一致,则响应结果正确;参考代码如下:

if(responseCode.code === 200 & responseBody !== null & responseBody.has("cal_name") ){ // 判断是否存在 'success' 值 tests["Body matches cal_name"] = responseBody.has("cal_name"); var jsonData = JSON.parse(responseBody); //保存食物AI名称 pm.globals.set("ai_cal_name", jsonData[0].properties.cal_name); pm.globals.get("ai_cal_name") console.log(pm.globals.get("ai_cal_name")) //保存食物输入名称 pm.globals.set("ai_name", jsonData[0].properties.name); pm.globals.get("ai_name") console.log(pm.globals.get("ai_name")) if(pm.globals.get("ai_cal_name")===pm.globals.get("ai_name")){ console.log("success") }else{ console.log("false") } tests["名称是否一致"]=pm.globals.get("ai_cal_name")===pm.globals.get("ai_name") //postman.setGlobalVariable("bk_id",jsonData.id); tests[`[INFO] Request params: ${JSON.stringify(request.id)}`] = true; //tests["have result "]=jsonData.hasOwnProperty("error")!==true; //tests["have cal_name "]=jsonData.hasOwnProperty("cal_name")===true; tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000; }else{ //接口请求失败 tests["Waring:Request Failed. Please Fix!"] = false; }

单独一个食物,接口测试就这样操作完成了,但是如果需要N个食物,那么该如何操作呢?所以这里就用到了类似jmeter中的csv的参数化的方式,可以直接把需要的食物名称保存在本地文件中,格式如下

[ {"msg":"干煸四季豆"}, {"msg":"红油蒜泥肘花"}, {"msg":"铁板豆腐"}, {"msg":"口水猪肝"}, {"msg":"魔芋鸭子"}, {"msg":"麻辣鱼"}, {"msg":"魔芋烧鸭"}, {"msg":"鱼香杏鲍菇"}, {"msg":"重庆豆花鱼"}, {"msg":"麻婆豆腐"}, {"msg":"四川泡菜鱼"}, {"msg":"四川烧白"}, {"msg":"姜爆鸭子"}, {"msg":"麻辣冷吃兔"}, {"msg":"香辣豉香干锅花菜"}, {"msg":"口水鸡"}, {"msg":"重庆酸菜鱼"}, {"msg":"豆花鸡"}, {"msg":"什锦凤爪"}, {"msg":"海鲜粉丝煲"}, {"msg":"金针菇培根卷"}, {"msg":"糖醋里脊"}, {"msg":"糖醋荷包蛋"}, {"msg":"椒麻鸡"}, {"msg":"芝麻酱秋葵"}, {"msg":"干锅辣子鸡"}, {"msg":"清蒸多宝鱼"}, {"msg":"丝瓜木耳炒鸡蛋"}, {"msg":"土豆炖大排"}, {"msg":"肉末番茄豆腐"}, {"msg":"豇豆炒肉丝"}, {"msg":"拔丝地瓜"}, {"msg":"红烧茄子"}, {"msg":"香辣土豆丝"}, {"msg":"鸡丝青瓜凉面"}, {"msg":"白灼秋葵"}, {"msg":"土豆红烧肉"}, {"msg":"三文鱼南瓜米粉"}, {"msg":"宫保鸡丁"}, {"msg":"可乐鸡翅"}, {"msg":"五香毛豆"}, {"msg":"肉丝焖面"}, {"msg":"剁椒鱼头"}, {"msg":"蒜香香菇"}, {"msg":"芙蓉虾仁"}, {"msg":"白灼牛肉"}, {"msg":"白切鸡"}, {"msg":"腊肠煲仔饭"}, {"msg":"鸡肉河粉"}, {"msg":"椰子炖鸡"}, {"msg":"炒黄豆芽"}, {"msg":"白灼虾"}, {"msg":"三蔬超意大利面"}, {"msg":"蒸酿苦瓜"}, {"msg":"韭菜炒鸡蛋"}, {"msg":"土豆炖大白菜"}, {"msg":"麻辣鸡腿"}, {"msg":"山楂糖葫芦"}, {"msg":"冷面"}, {"msg":"茄子炖豆角"}, {"msg":"地三鲜"}, {"msg":"炸酱面"}, {"msg":"西红柿炖羊排"}, {"msg":"烧饼"}, {"msg":"五花肉炖四季豆"}, {"msg":"酸辣土豆丝"}, {"msg":"蚝油排骨"}, {"msg":"香菇土豆炖鸡腿"}, {"msg":"酸菜猪肉饺子"}, {"msg":"蜂蜜麻花"}, {"msg":"四喜丸子"}, {"msg":"小鸡炖蘑菇"}, {"msg":"锅包肉"}, {"msg":"红烧肉"}, {"msg":"猪肉炖粉条"}, {"msg":"水晶鲑鱼"}, {"msg":"白豆腐"}, {"msg":"红烧鹿肉"}, {"msg":"五香酱牛肉"}, {"msg":"糖醋苦瓜"}, {"msg":"西红柿焖牛肉"}, {"msg":"火腿白菜"} ]

这里是单独一个入参,如果是多个入参,可以根据格式修改,格式要求是[],那么接下来就是讲代码中的如下名称修改为变量

msg:{{msg}}

然后接下来就是postman中操作:

点击"Runner"

选择需要批量执行的接口

选中参数化的文件,可以点击"Preview"预览数据信息

如下所示:

postman 接口参数化操作

如下图,显示的就是批量运行接口的结果,这样相对来说比较容易统计出错率,计算模型准确率比较简单~

postman 接口参数化操作

总结:以上就是postman参数化的简单操作,相对来说比较简单~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsdyd.html