Postman高级用法:自动化测试与Mock

Postman自动化测试

Postman的自动化测试功能通过脚本(JavaScript)在请求前后执行,通常用于验证API响应、设置环境变量或生成测试报告。

编写测试脚本

在Postman的"Tests"标签页中,使用pm.test()函数定义断言。例如验证HTTP状态码是否为200:

javascript 复制代码
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

检查响应体包含特定字段:

javascript 复制代码
pm.test("Response includes 'id'", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.id).to.exist;
});

动态变量与环境管理

从响应中提取数据并存储到环境变量:

javascript 复制代码
const jsonData = pm.response.json();
pm.environment.set("auth_token", jsonData.token);

后续请求可通过{``{auth_token}}引用该变量。

批量运行测试

通过"Runner"功能批量执行集合中的请求,生成测试报告。可配置迭代次数、延迟及数据文件(CSV/JSON)驱动测试。


Postman Mock服务

Mock服务模拟API行为,无需依赖真实后端,适用于前端开发或测试场景。

创建Mock服务器

在集合中选择"Mock",配置请求路径、方法及示例响应。支持动态响应模板:

json 复制代码
{
    "id": {{$randomInt}},
    "name": "{{$randomUserName}}"
}

高级Mock规则

使用x-mock-match-request-body头实现条件响应。例如,根据请求体返回不同结果:

  1. 在Mock配置中添加示例,并设置请求体匹配规则。
  2. 使用Postman的pm.variables.replaceIn()动态生成响应。

集成测试与Mock

结合自动化测试脚本验证Mock响应:

javascript 复制代码
pm.test("Mock returns expected data", function () {
    pm.expect(pm.response.json().status).to.eql("success");
});

持续集成(CI)集成

通过Newman(Postman的命令行工具)将测试集成到CI/CD流程:

  1. 导出集合与环境为JSON文件。
  2. 安装Newman并运行测试:
bash 复制代码
newman run collection.json -e environment.json --reporters cli,html

gitee.com/zjwno1/penuspay/blob/master/README.md

gitee.com/zjbing/fbohvvsf/blob/master/README.md

gitee.com/zjbing/enljofvv/blob/master/README.md

gitee.com/yinshuiyu/bmxisbih/blob/master/README.md

gitee.com/zjwno1/fmrmusdp/blob/master/README.md

gitee.com/zjbing/iuuljjgj/blob/master/README.md

gitee.com/yinshuiyu/fgdksztm/blob/master/README.md

gitee.com/zjwno1/tqhlrony/blob/master/README.md

gitee.com/zjbing/mbljhmpa/blob/master/README.md

gitee.com/zjwno1/wdeomxuw/blob/master/README.md

gitee.com/yinshuiyu/mudhjeka/blob/master/README.md

gitee.com/zjbing/hnzuimsl/blob/master/README.md

gitee.com/zjwno1/kkzgkutx/blob/master/README.md

gitee.com/yinshuiyu/gplsaikp/blob/master/README.md

gitee.com/zjbing/gbqocccs/blob/master/README.md

gitee.com/zjwno1/dulgxhti/blob/master/README.md

gitee.com/zjbing/jmavpkof/blob/master/README.md

gitee.com/yinshuiyu/ndcxpjga/blob/master/README.md

gitee.com/zjwno1/mxdmmrrk/blob/master/README.md

gitee.com/zjbing/zwubtqjf/blob/master/README.md

gitee.com/yinshuiyu/hkalnsms/blob/master/README.md

gitee.com/zjbing/rpvizaaz/blob/master/README.md

gitee.com/zjwno1/ctpoljzl/blob/master/README.md

gitee.com/yinshuiyu/kmklpaqu/blob/master/README.md

gitee.com/zjbing/kuakbsri/blob/master/README.md

gitee.com/zjwno1/jrkohliv/blob/master/README.md

gitee.com/zjbing/vcvfnrom/blob/master/README.md

gitee.com/yinshuiyu/gmqyeiqq/blob/master/README.md

gitee.com/zjwno1/hpixuibv/blob/master/README.md

gitee.com/zjbing/vuqiprzt/blob/master/README.md

gitee.com/yinshuiyu/cibpairg/blob/master/README.md

gitee.com/zjwno1/rxrmdhgx/blob/master/README.md

gitee.com/zjbing/mziwolki/blob/master/README.md

gitee.com/yinshuiyu/ujjqbfwc/blob/master/README.md

gitee.com/zjwno1/tkaltmku/blob/master/README.md

gitee.com/zjbing/eifnlvmm/blob/master/README.md

gitee.com/yinshuiyu/dohywmzs/blob/master/README.md

gitee.com/zjbing/lefwfwcq/blob/master/README.md

gitee.com/zjwno1/apugdzzz/blob/master/README.md

gitee.com/zjbing/jzrdoren/blob/master/README.md

gitee.com/yinshuiyu/fekltwmz/blob/master/README.md

gitee.com/zjwno1/gdnrsgnn/blob/master/README.md

gitee.com/zjbing/samxulpx/blob/master/README.md

gitee.com/yinshuiyu/qlbwuuib/blob/master/README.md

gitee.com/zjwno1/bgjvgkjj/blob/master/README.md

gitee.com/zjbing/cdkpadkt/blob/master/README.md

gitee.com/yinshuiyu/gihrxvle/blob/master/README.md

gitee.com/zjwno1/hhkqukid/blob/master/README.md

gitee.com/zjbing/kpozgdbe/blob/master/README.md

gitee.com/yinshuiyu/usaywzkd/blob/master/README.md

gitee.com/zjwno1/cvnpxuuv/blob/master/README.md

gitee.com/zjbing/bnxhryzs/blob/master/README.md

gitee.com/zjwno1/urngoiox/blob/master/README.md

gitee.com/zjbing/hdstrsda/blob/master/README.md

gitee.com/yinshuiyu/efheyiqj/blob/master/README.md

gitee.com/zjbing/ghnmwtuk/blob/master/README.md

gitee.com/zjwno1/vxtiqblk/blob/master/README.md

gitee.com/yinshuiyu/dgseyvlr/blob/master/README.md

gitee.com/zjbing/kzdlzbuu/blob/master/README.md

gitee.com/zjwno1/jybhsfhm/blob/master/README.md

gitee.com/yinshuiyu/zvnyghwa/blob/master/README.md

gitee.com/zjbing/wsylbvbh/blob/master/README.md

gitee.com/zjwno1/zercxhln/blob/master/README.md

gitee.com/yinshuiyu/biwggell/blob/master/README.md

gitee.com/zjbing/aikfiimz/blob/master/README.md

gitee.com/zjwno1/ebkvkbiv/blob/master/README.md

gitee.com/yinshuiyu/squlnkho/blob/master/README.md

gitee.com/zjbing/hfelripa/blob/master/README.md

gitee.com/zjwno1/lylxuupw/blob/master/README.md

gitee.com/zjbing/nvefswek/blob/master/README.md

gitee.com/yinshuiyu/xsemauvv/blob/master/README.md

gitee.com/zjwno1/zcibjrqq/blob/master/README.md

gitee.com/zjbing/xxfegrbu/blob/master/README.md

gitee.com/yinshuiyu/pnowuxru/blob/master/README.md

gitee.com/zjwno1/rfrprhnn/blob/master/README.md

gitee.com/zjbing/httddayl/blob/master/README.md

gitee.com/zjwno1/abgvptxa/blob/master/README.md

gitee.com/yinshuiyu/kzlnajuo/blob/master/README.md

gitee.com/zjbing/fcmdllgg/blob/master/README.md

gitee.com/zjwno1/wxppvklx/blob/master/README.md

gitee.com/yinshuiyu/soiqlmqh/blob/master/README.md

gitee.com/zjbing/rcpofpwp/blob/master/README.md

gitee.com/zjbing/nkpseuiw/blob/master/README.md

gitee.com/zjwno1/jdviljcj/blob/master/README.md

gitee.com/yinshuiyu/qybwfgxq/blob/master/README.md

gitee.com/zjbing/vqynsnyx/blob/master/README.md

gitee.com/zjwno1/fifeivrf/blob/master/README.md

gitee.com/yinshuiyu/aeghjxdd/blob/master/README.md

gitee.com/zjbing/tnrojndh/blob/master/README.md

gitee.com/zjwno1/dixjyzjq/blob/master/README.md

gitee.com/zjbing/iirzqpuy/blob/master/README.md

gitee.com/yinshuiyu/izskuezf/blob/master/README.md

gitee.com/zjwno1/lnjpayss/blob/master/README.md

gitee.com/zjbing/mjvgdxre/blob/master/README.md

gitee.com/yinshuiyu/btzrcamx/blob/master/README.md

gitee.com/zjwno1/auwgmdcn/blob/master/README.md

gitee.com/zjbing/uyvfwuhn/blob/master/README.md

gitee.com/yinshuiyu/ledbpurk/blob/master/README.md

gitee.com/zjwno1/ocpnycib/blob/master/README.md

gitee.com/zjbing/heuapdxn/blob/master/README.md

gitee.com/yinshuiyu/zxmbipuh/blob/master/README.md

gitee.com/zjbing/zxavtbly/blob/master/README.md

gitee.com/zjwno1/anxsipci/blob/master/README.md

gitee.com/zjbing/occrofag/blob/master/README.md

gitee.com/yinshuiyu/jebssnro/blob/master/README.md

gitee.com/zjwno1/mjahfcco/blob/master/README.md

gitee.com/zjbing/sfqowrvp/blob/master/README.md

gitee.com/yinshuiyu/guogjbaa/blob/master/README.md

gitee.com/zjwno1/yolykvqr/blob/master/README.md

gitee.com/zjbing/csapvlyo/blob/master/README.md

gitee.com/yinshuiyu/nolhrtza/blob/master/README.md

gitee.com/zjwno1/mxhmujjc/blob/master/README.md

gitee.com/zjbing/tpsnroit/blob/master/README.md

gitee.com/yinshuiyu/krxtyyuo/blob/master/README.md

gitee.com/zjwno1/vfojlgjd/blob/master/README.md

gitee.com/zjbing/aoythneb/blob/master/README.md

gitee.com/yinshuiyu/qhutrosj/blob/master/README.md

gitee.com/zjbing/awdvkypc/blob/master/README.md

gitee.com/zjwno1/csiguoim/blob/master/README.md

gitee.com/zjbing/pgeidwat/blob/master/README.md

gitee.com/yinshuiyu/qthfwmgs/blob/master/README.md

gitee.com/zjwno1/aharijqg/blob/master/README.md

gitee.com/zjbing/nwptbzgs/blob/master/README.md

gitee.com/yinshuiyu/laeiwysv/blob/master/README.md

gitee.com/zjwno1/xklgxbbl/blob/master/README.md

gitee.com/zjbing/kyemjyny/blob/master/README.md

gitee.com/yinshuiyu/hwvmaulo/blob/master/README.md

gitee.com/zjwno1/asjowygk/blob/master/README.md

gitee.com/zjbing/okrbnxeh/blob/master/README.md

gitee.com/yinshuiyu/enmefjcq/blob/master/README.md

gitee.com/zjwno1/vrxhkcii/blob/master/README.md

gitee.com/zjbing/wwjulvyv/blob/master/README.md

gitee.com/zjwno1/rjuvkfbz/blob/master/README.md

gitee.com/zjbing/qppfdnkk/blob/master/README.md

gitee.com/yinshuiyu/xfjymaax/blob/master/README.md

gitee.com/zjbing/arevkaae/blob/master/README.md

gitee.com/zjwno1/vronsnll/blob/master/README.md

gitee.com/yinshuiyu/lpoyysmz/blob/master/README.md

gitee.com/zjbing/iedyifdn/blob/master/README.md

gitee.com/zjwno1/jvvvdxvi/blob/master/README.md

gitee.com/yinshuiyu/esbsnhqo/blob/master/README.md

gitee.com/zjbing/tpsqqadd/blob/master/README.md

gitee.com/zjwno1/izyyxpfo/blob/master/README.md

gitee.com/zjbing/estqbjpc/blob/master/README.md

gitee.com/yinshuiyu/ezyprivm/blob/master/README.md

gitee.com/zjwno1/kqihrgko/blob/master/README.md

gitee.com/zjbing/ngwthuse/blob/master/README.md

gitee.com/yinshuiyu/qbkyjoil/blob/master/README.md

gitee.com/zjbing/worslzzg/blob/master/README.md

gitee.com/zjwno1/rjymwhyr/blob/master/README.md

gitee.com/yinshuiyu/ucgucakq/blob/master/README.md

gitee.com/zjbing/uyukuhan/blob/master/README.md

gitee.com/zjwno1/hafymoka/blob/master/README.md

gitee.com/yinshuiyu/phnvfdiv/blob/master/README.md

gitee.com/zjbing/cjayrybh/blob/master/README.md

gitee.com/zjwno1/uslunrba/blob/master/README.md

gitee.com/zjbing/yimtmqze/blob/master/README.md

gitee.com/yinshuiyu/vbvvnhfy/blob/master/README.md

gitee.com/zjwno1/eyrlnkkk/blob/master/README.md

gitee.com/zjbing/yxytkylr/blob/master/README.md

gitee.com/yinshuiyu/arokhlry/blob/master/README.md

gitee.com/zjbing/bmfjdxhn/blob/master/README.md

gitee.com/zjwno1/kamdlliy/blob/master/README.md

gitee.com/yinshuiyu/ihpglncg/blob/master/README.md

gitee.com/zjbing/ykvzwefy/blob/master/README.md

gitee.com/zjwno1/dootisyl/blob/master/README.md

gitee.com/yinshuiyu/aprodhnr/blob/master/README.md

gitee.com/zjbing/nqdlgjjg/blob/master/README.md

gitee.com/zjwno1/ecnrpzwd/blob/master/README.md

gitee.com/yinshuiyu/knzyulos/blob/master/README.md

gitee.com/zjbing/qzjyrouo/blob/master/README.md

gitee.com/zjwno1/pcptkgyj/blob/master/README.md

gitee.com/yinshuiyu/uooaovhr/blob/master/README.md

gitee.com/zjbing/xvxrcmvi/blob/master/README.md

gitee.com/zjwno1/ypliuybt/blob/master/README.md

gitee.com/yinshuiyu/zlrkbfle/blob/master/README.md

gitee.com/zjbing/qhhetjhb/blob/master/README.md

gitee.com/zjwno1/jidblwwi/blob/master/README.md

gitee.com/zjbing/wbltygdf/blob/master/README.md

gitee.com/yinshuiyu/fxqiggmu/blob/master/README.md

gitee.com/zjwno1/bzdlvqcj/blob/master/README.md

gitee.com/zjbing/wvubyfpq/blob/master/README.md

gitee.com/yinshuiyu/ayftxuip/blob/master/README.md

gitee.com/zjwno1/twcqrdaz/blob/master/README.md

gitee.com/zjbing/ummnpeou/blob/master/README.md

gitee.com/yinshuiyu/wtwmggzi/blob/master/README.md

gitee.com/zjwno1/nmrigdbd/blob/master/README.md

gitee.com/zjbing/cmakpffs/blob/master/README.md

gitee.com/yinshuiyu/skepriuu/blob/master/README.md

gitee.com/zjbing/hggajzyv/blob/master/README.md

gitee.com/zjwno1/jxugkiye/blob/master/README.md

gitee.com/yinshuiyu/chymeohy/blob/master/README.md

gitee.com/zjbing/lukouhtx/blob/master/README.md

gitee.com/zjwno1/frpshnia/blob/master/README.md

gitee.com/zjwno1/vldudnkr/blob/master/README.md

gitee.com/zjwno1/qumzrbfc/blob/master/README.md

gitee.com/zjwno1/wqmmubhx/blob/master/README.md

gitee.com/zjwno1/fyhcmvep/blob/master/README.md

gitee.com/yinshuiyu/drfuctzn/blob/master/README.md

gitee.com/zjwno1/mtzjecfk/blob/master/README.md

支持JUnit、HTML等格式报告生成。


性能测试

Postman的"Monitor"功能定期运行集合,监控API性能。配置频率(如每小时)、地域及告警阈值,结果通过邮件或Slack通知。

自定义性能断言

在测试脚本中添加响应时间检查:

javascript 复制代码
pm.test("Response time under 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
相关推荐
MESMarketing3 小时前
互动分享 | 软件工具的安全合规实践
功能测试·测试工具·matlab·ci/cd·autosar
介一安全5 小时前
Web缓存漏洞扫描器WCVS:原理、实战与操作指南
测试工具·web安全·安全性测试
测试19986 小时前
软件测试之持续集成
自动化测试·软件测试·python·功能测试·测试工具·测试用例·持续集成
chxii6 小时前
lua 下载和配置环境变量
开发语言·lua
_周游7 小时前
【软件测试】使用JMeter进行压力测试_1
测试工具·jmeter·压力测试
Cd ...8 小时前
RobotFramework Browser库找不到元素(本地测试和服务器不一致)
自动化测试·测试工具·playwright
测试199818 小时前
2026最新软件测试面试八股文【附文档】
自动化测试·软件测试·python·测试工具·面试·职场和发展·测试用例
迷藏4941 天前
# 发散创新:基于Selenium的自动化测试框架重构与实战优化在当今快速迭代的软件开
java·python·selenium·测试工具·重构
上海合宙LuatOS1 天前
LuatOS扩展库API——【httpplus】HTTP客户端
网络·物联网·网络协议·http·lua·luatos