postman使用教程7-参数化引用外部文件(txt/csv/json)测试数据

前言

当同一个接口需要测试不同的参数时,需用到参数化的概念。postman支持从外部文件读取测试数据参数化

设置参数化变量

新建一个collections 专门测试登录接口

设置集合变量username和password

可以给个初始值,方便调试通过

Collection Runner

点集合后面的三角形按钮

进入Collection Runner 页面

  • Environment 选择运行环境
  • Iterations 迭代次数,也就是参数化数据的次数
  • Delay 延迟毫秒,每次迭代中间的延迟时间
  • Log Response 查看response返回的log日志
  • Data 选择参数化文件(.txt/.csv/.json)
  • Keep variables values 每次迭代后保持变量的值
  • Run collection whitout using stored cookies 不保存cookies
  • Svae cookies after collection run 运行完集合保存cookies

参数化登录

先准备需要参数化的测试数据,前面定义了2个变量username和password,于是在txt文本里面第一行写变量的名称,后面换行分别写需要传入的值

username,password
test1,123456
test2,123456
test3,123456
test4,123456
test5,123456

保存文件名称为username_password.txt

Data File Type 选文件类型:text/csv

Preview 按钮可以查看参数化数据对不对

点Run 按钮运行,接着可以看到登录接口运行了5次

获取变量

要在“Test”或“Pre-request Script”代码中使用数据文件中的值,请使用iterationData,以提供对用于运行请求的当前数据文件记录的访问。

//get the 'value' field from the data file for this request run
pm.iterationData.get("value")

可以通过console.log()在控制台输出打印日志

var a = pm.iterationData.get("username");
console.log(a)

作者-上海悠悠 blog地址 https://www.cnblogs.com/yoyoketang/

posted @ 2021-05-08 13:20  上海-悠悠  阅读(2250)  评论(0编辑  收藏  举报