基于 Postman 实现 API 自动化测试

科技资讯 投稿 42900 0 评论

基于 Postman 实现 API 自动化测试

使用说明

我们以测试百度的 https://www.baidu.com/sugrec 这个 API 作为示例。

1. 创建 Collection

新建请求:

测试请求,状态码返回 200:

编写测试断言并验证断言:

pm.test("Return 200", function( {    pm.response.to.have.status(200 }

2. 创建 Environment

填写参数信息,这里简单使用 host 作为参数:

在 collection 的请求中使用 host 参数,并进行测试:

3. 导出 Collection 和 Environment

导出 environment 为 environment.json:

4. 执行 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 报告:

5. 集成 CI 实现 API 自动化测试

以上就是本文希望分享的内容,如果大家有什么问题,欢迎在文章或者公众号 - 跬步之巅留言交流。

编程笔记 » 基于 Postman 实现 API 自动化测试

赞同 (35) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽