当前位置:网站首页>最牛最全的 Postman 实现 API 自动化测试教程
最牛最全的 Postman 实现 API 自动化测试教程
2022-08-10 03:38:00 【测试架构师北凡】
【 文章末尾给大家留下了大量的福利】
背景介绍
相信大部分开发人员和测试人员对 postman 都十分熟悉,对于开发人员和测试人员而言,使用 postman 来编写和保存测试用例会是一种比较方便和熟悉的方式。但 postman 本身是一个图形化软件,相对较难或较麻烦(如使用 RPA)实现自动化测试。幸运的是,postman 还提供了一个命令行工具 newman,我们可以借助 postman + newman 来实现 API 自动化测试。
名词解析
1. Collection
Collection 是一组保存的请求,postman 中发送的每个请求都会显示在侧栏的 ”历史记录“ 选项卡下。请求数量比较少的时候,通过历史记录来重用请求会比较方便。随着请求量的增长,在历史记录中查找特定请求可能会非常耗时。此时,你可以将所有请求保存为一个 collection,以便于访问。
2. Environment
Environment 是一组可以在 postman 请求中使用的变量。你可以根据不同的环境(如 dev、test、prod 等),把一组相关的变量放到不同的 environment 文件中,来对不同的环境进行 API 自动化测试。
使用说明
我们以测试百度的 https://www.baidu.com/sugrec 这个 API 作为示例。
1. 创建 Collection
新建 collection:
新建请求:
测试请求,状态码返回 200:
编写测试断言并验证断言:
pm.test("Return 200", function() { pm.response.to.have.status(200) })
2. 创建 Environment
新建 environment:
填写参数信息,这里简单使用 host 作为参数:
在 collection 的请求中使用 host 参数,并进行测试:
3. 导出 Collection 和 Environment
导出 collection 为 collection.json:
导出 environment 为 environment.json:
3. 执行 API 测试
我们只需要基于导出的 collection.json 和 environment.json 执行一条 docker 指令即可完成 API 测试:
docker run --rm -i -v /root/postman:/etc/newman \ --entrypoint sh postman/newman:alpine -c \ 'npm i -g newman-reporter-html; \ newman run collection.json \ --suppress-exit-code 1 \ --color off \ --reporters cli,html\ --reporter-html-export api_report.html \ --environment=environment.json'
指令解析:
项目 | 说明 |
docker run | 启动 docker 容器 |
--rm | 退出容器时销毁容器 |
-i | 交互模式 |
-v /root/postman:/etc/newman | 目录挂载 |
--entrypoint sh postman/newman:alpine -c | 容器执行指令 |
npm i -g newman-reporter-html | 安装 html 报告插件 |
newman run collection.json | 指定测试 collection.json |
--suppress-exit-code 1 | 指定错误状态码为 1 |
--color off | 关闭颜色 |
--reporters cli,html | 输出命令行和 html 报告 |
--reporter-html-export api_report.html | 设置输出 html 文件名 |
--environment=environment.json | 指定 environment.json 文件 |
执行结果:
查看 html 报告:
4. 集成 CI 实现 API 自动化测试
你只需要针对你的代码项目准备好 collection.json 和 environment.json,把它们存放到代码中的某个目录,然后在 jenkins 或 gitlab-ci 中添加执行上面介绍的 docker 指令即可。
重点:学习资料学习当然离不开资料,这里当然也给你们准备了600G的学习资料
需要的先关注再私我关键字【000】免费获取哦 注意关键字是:000
疑惑:为什么要先关注呢? 回:因为没关注的话私信回了你看不到
项目实战
app项目,银行项目,医药项目,电商,金融
大型电商项目
全套软件测试自动化测试教学视频
300G教程资料下载【视频教程+PPT+项目源码】
全套软件测试自动化测试大厂面经
python自动化测试++全套模板+性能测试
听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!!
边栏推荐
猜你喜欢
Software life cycle (the work of each phase of software engineering)
zabbix添加监控主机和自定义监控项
It's almost 35, still "did a little"?What happened to the test workers who had been in the industry for a few years?
自定义训练,使用Generator dataset迭代数据报错
Difference between netstat and ss command
Dijkstra求最短路
如何快速成为一名软件测试工程师?测试员月薪15k需要什么技术?
全面深入了解什么是反向代理和负载均衡
数据切片问题
金融口译,口译中金融高频词有哪些
随机推荐
mindspore gpu版本安装问题
10个超赞的C语言开源项目,值得学习
mindspore安装过程中报错cannot find zlib
TCP协议之《发送缓存控制tcp_notsent_lowat》
program internationalization
金融财经翻译的行业前景如何
It's almost 35, still "did a little"?What happened to the test workers who had been in the industry for a few years?
一个刚入行的测试员怎么样做好功能测试?测试思维真的很重要
【2022河南萌新联赛第(五)场:信息工程大学】【部分思路题解+代码解析】
The same is a primary test, why does he pay 5,000 yuan more than me?
小程序分包及分包预下载
golang go get 时提示 no Go files in xxx
Qt 线程常用同步方式
sql优化
matlab simulink response spectrum calculation
goland console shows overlapping problem solution
线程执行测试效果
黑马jvm课程笔记d2
C语言结构体初识
TCP协议之《ACK状态4种详解》