{{$timestamp}}:当前时间戳;如下图

可通过在线时间戳转换工具查看时间:https://tool.lu/timestamp/

postman body test 传参_lua

{{$randomInt}}:0-1000的随机整数

postman body test 传参_软件测试_02


二、Pre-request Script页签中使用代码实现(推荐)
Pre-request Script为执行接口请求之前要做的操作,而tests是执行完请求要 做的操作。内建变量一般放在Request里,我们也可以用代码在Pre-request Script中实现,用代码实现的好处是可以复用。

Pre-request Script 与test 一样 支持javascript语法

postman body test 传参_Math_03

Pre-request Script中实现的几种参数化;如下图****

//获取当前时间戳 毫秒
 var now_time = Date.now()
 pm.globals.set(“now_time”,now_time)//guid实现
 const guid = ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’
 .replace(/x/g, () => (Math.floor(Math.random() * 16)).toString(16))
 .replace(/y/g, () => (Math.floor(Math.random() * 4 + 8)).toString(16));
 pm.globals.set(“guid_value”,guid)//随机整数实现
 const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) +
 min
 pm.globals.set(“randomInt_num”,randomInt(8,15))//从多个选项中选择实现
 const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) +
 min
 const getRandomValue = list => list[randomInt(0, list.length - 1)];
 const charsInName = [‘王’,‘李’,‘张’]
 pm.globals.set(“people_name”,getRandomValue(charsInName))//随机手机号实现
 const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) +
 min
 var mobile_num = 18${randomInt(100000000, 999999999)};
 pm.globals.set(“mobile_num”,mobile_num)//同步等待实现:等待5秒后开始执行
 const sleep = (milliseconds) => {
 const start = Date.now();
 while (Date.now() <= start + milliseconds) {}
 }
 sleep(5000)

实战示例:通过Pre-request Script编写代码可以在随机参数中做断言的精确比对****

1、在Pre-request Script中编写代码

postman body test 传参_postman_04

2、在请求中引用变量;{{变量名}}

postman body test 传参_Math_05

 3、在tests中做断言,验证实际结果和预期结果是否一致

postman body test 传参_测试工具_06

4、查看执行结果

postman body test 传参_Math_07

三、外部文件方式实现参数化
在postman中,除了上述两种方法实现参数化,还可以利用外部数据文件 (支持csv文件和包含json格式文本的数据)。外部数据文件目前是通过Runner 页签可以进行导入。

csv文件格式举例:

csv文件要在Collection Runner中工作,第一行必须是在request中要使用的 变量名,每一行是一条用例,并表示一次迭代。

postman body test 传参_软件测试_08

1、先准备csv文件,编码是UTF-8;如上图

2、在postman中引用变量名,如下图中的百度搜索中wd的参数引入了{{search_word}},tests页签中做断言引用了data.expected_result

postman body test 传参_lua_09

postman body test 传参_软件测试_10

 3、点击Runner按钮,勾选需执行的请求,在设置循环次数,导入csv文件,点击run执行;如下图

postman body test 传参_lua_11

4、查看执行结果

postman body test 传参_Math_12