当前位置:网站首页>postman之参数化详解
postman之参数化详解
2022-07-06 08:57:00 【自动化测试七叔】
前言
小伙伴们,好久不见呀,今天呢笔者想和大家聊聊postman参数化,在接口测试中,部分参数每次发送请求是唯一的数值,这时可以采用把测试数据参数化处理的方式实现,postman参数化的形式主要有三种。
1、内置变量实现
内建变量为postman自带的系统变量,但是不能进行精确化的对比
Variable Name | Description | Examples |
{ {$guid}} | 添加一个V4风格的GUID | “611c2e81-2ccb-42d8-9ddc-2d0bfa65c1b4” |
{ {$timestamp}} | 添加当前的时间戳,精确到秒 | 1562757107 , 1562757108 , 1562757109 |
{ {$randomint}} | 添加0-1000之间的随机整数 | 0-1000之间的随机整数 |
{ {$randomFullName}} | 随机全名(名字 姓氏) | Connie Runolfsdottir , Sylvan Fay , Jonathon Kunze |
{ { $randomBoolean}} | 随机布尔值 (true/false) | true , false , false , true |
更多内置变量参考:https://blog.csdn.net/minzhung/article/details/102531880 内建变量一般放在request里
2、Pre-request-Script页签中使用代码实现
单一请求执行顺序如下图所示:

Pre-request-Script→request→response→Tests
这是比较推荐的一种参数化方法,可以通过自己写脚本的方式,提高了可塑性,
值得注意的是接口测试执行时接口会先执行这里的代码,然后发送请求,最后进行断言,与test一样都支持js语法
Pre-request-Script为执行接口请求之前要做的操作,而tests是执行完请求要做的操作,内建变量一般放在request里,我们
也可以用代码在pre-request-Script中实现,用代码实现的好处是可以复用,以下为部分举例:
示例一、Pre-request-Script先定义当前时间并获取,把当前时间设置为全局变量后传入请求参数中,然后在Tests中进行断言


3、下面是其他部分参数化方式的实现

代码如下
var now_time = Date.now();
pm.globals.set("now_time", now_time);
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("random_number", randomInt(8,14) );
const getRandomValue = list => list[randomInt(0, list.length - 1)];
const charsInName = ['王','李','张'];
pm.globals.set("people_name", getRandomValue(charsInName) );
var mobile_num = `13${randomInt(500000000, 599999999)}`;
pm.globals.set('mobile_num',mobile_num);
const sleep = (milliseconds) => {
const start = Date.now();
while (Date.now() <= start + milliseconds) {}
};
sleep(5000);4、外部文件的方式实现
4.1通过csv文件或者json格式的文件实现参数化
csv的运用,首先通过在文件中设置需要搜索的字段和搜索出来的结果

关键自之间需要用英文逗号分隔开,第一行写定义的参数变量,切记需要以utf-8的编码格式、.csv的文件后缀名保存,点击runner按钮


也可以在控制台查看具体运行结果

断言的期望结果如果也写入文件中的话,也可以在Tests断言中调用,这里不再举例

4.2外部文件也可以通过json格式的文件实现参数化,导入文件的时候记得校验文件是否为json


值得注意的是iteration设置迭代次数,每次迭代都会使用你的数据文件中的一行,如果迭代次数超过数据文件中的行数,讲会重复使用最后一行数据
总结
今天的文章就分享到这里了,喜欢的小伙伴记得点赞收藏评论加关注哟,关注我每天给你不同的惊喜。

边栏推荐
- LeetCode:498. 对角线遍历
- Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
- Simclr: comparative learning in NLP
- 有效提高软件产品质量,就找第三方软件测评机构
- KDD 2022 paper collection (under continuous update)
- MongoDB 的安装和基本操作
- LeetCode:498. Diagonal traversal
- [OC]-<UI入门>--常用控件的学习
- LeetCode:387. 字符串中的第一个唯一字符
- 如何正确截取字符串(例:应用报错信息截取入库操作)
猜你喜欢

Excellent software testers have these abilities

Variable length parameter

TP-LINK enterprise router PPTP configuration

多元聚类分析

Advanced Computer Network Review(3)——BBR

自定义卷积注意力算子的CUDA实现

704 binary search

Alibaba cloud server mining virus solution (practiced)

requests的深入刨析及封装调用

Compétences en mémoire des graphiques UML
随机推荐
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
Detailed explanation of dynamic planning
LeetCode:236. 二叉树的最近公共祖先
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
Tdengine biweekly selection of community issues | phase III
在QWidget上实现窗口阻塞
Mise en œuvre de la quantification post - formation du bminf
LeetCode:41. Missing first positive number
【剑指offer】序列化二叉树
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Pytorch view tensor memory size
opencv+dlib实现给蒙娜丽莎“配”眼镜
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
TDengine 社区问题双周精选 | 第三期
Advance Computer Network Review(1)——FatTree
UML diagram memory skills
有效提高软件产品质量,就找第三方软件测评机构
Promise 在uniapp的简单使用