Postman实现接口自动化测试

 

一:Postman安装

1. 环境搭建:

1) 安装 Postman
下载完成后,选择默认项进行安装。
注册登录,建议使用邮箱账号注册,数据共享且易找回密码。

二:Postman页面介绍

Postman实现接口自动化测试_第1张图片

 

1.请求

1)请求方式;
2)url;
3)参数;
4)body;
5)预计脚本;
6)断言

2.响应

3.管理接口

1)历史记录;
2)接口集合;
3)批量执行;--------可用来造数据,比Python脚本快(比如需要大量数据,查看翻页以及记载等功能)
批量执行接口,可以实现数据驱动测试,对于不同的断言结果,可使用if,else进行分类。上传的文件可以是text文件也可以是csv文件,建议用json的数据格式。
操作步骤:单击run按钮,打开collection runner页面,设置执行的脚本、环境、迭代次数等,点击run*开始执行。

Postman实现接口自动化测试_第2张图片

 

Postman实现接口自动化测试_第3张图片

 

批量执行的结果,可以通过数和失败数,断言成功或断言失败导出报告。

Postman实现接口自动化测试_第4张图片

 

4.变量

1)变量的添加:
右上角设置——添加环境变量

19534525-d0e0435e99eb9652.png (240×40)

 

2)变量的分类:
环境变量:变量值随环境不同而变化;如test和线上只是域名不同,可以通过切换环境,切换域名;
全局变量:全局有效,必须唯一,不可重复定义,如果重复定义则会更新变量值。
局部变量:针对单个需求设置的变量,只在该请求中有效

Postman实现接口自动化测试_第5张图片

 


集合比变量:在collection或文件夹中添加变量,作用于整个集合或问价夹中,不会随环境的改变而改变。

Postman实现接口自动化测试_第6张图片

 

3)变量的优先级:
local>data>environment>collection>global
注:data是数据文件,local是局部变量;

Postman实现接口自动化测试_第7张图片

 

4)使用环境变量
使用方法{{变量名}}

5.脚本(Script)

1)脚本的作用:
Node.js运行环境,可以通过编写js脚本来实现一些动态的行为;
1.动态参数
2.请求之间传递数据
3.帮助做自动化测试...
可以在请求前和请求后执行脚本

Postman实现接口自动化测试_第8张图片

 


2)使用场景
1.设置/获取/清除变量
pm.environment.get("variable_key");
pm.globals.get("variable_key");
pm.variables.get("variable_key");
pm.environment.set("variable_key","variable_value");
pm.globals.set("variable_key","variable_value");
pm.environment.unset("variable_key");
pm.globals.unset("variable_key");
2.处理响应
函数格式:
pm.test(testname:string,specfunction:function):function
校验响应状态
pm.test("Status code is 200",function(){
pm.response.to.have.status(200);
});
response是否包含指定字符串
pm.test("Body matches string",function(){
pm.expect(pm.response.text().to.include("请求成功"));
});
检查整个response body是否等于期望值
pm.test("Body is correct",function(){
pm.response.to.have.body("response_body_string");
});
检查header中是否存在Content-Type
pm.test("Content-Type is present",function(){
pm.response.to.have.header("Content-Type");
});
检查响应时间是否小于200ms
pm.test("Response time is less than 200ms",function(){
pm.expect(pm.response.responseTime).to.be.below(200);
});
3.处理断言
获取response
res=pm.response.json();
检查字符串中包含指定字符串
pm.test("Check if pattern is in target string",function(){
pm.expect('foobar').to.have.string('bar');
});
严格比较相等
const TEN = 10;
pm.test('Check if number is equal to 10',function(){
pm.expect(TEN).to.equal(10);
});
检查字段值为空(搜索为空)
pm.test('Check if array is empty',function(){
pm.expect([]).to.be.empty;
})
响应json,某个字段值是否等于14
pm.test("Your test name",function(){
var jsonData = pm.response.json();
pm.expect(jsonData.data.channelList[0].channelld.to.eql(14));
});
检查response字符串类型
pm.test("Check if target is number",function(){
res = pm.response.json();
data = res.data.channelList[0].channelld;
pm.expect(data).to.be.a('number');
});
4.脚本栗子展示
1)获取日期
var d = new Date();
tabletime = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
pm.environment.set("tabletime",tabletime);
2)延迟时间
setTimeout(function()){
console.log("延迟10秒");
},1000);
5.执行顺序
script也可以写在“集合”和“文件夹”中,执行顺序如图:

Postman实现接口自动化测试_第9张图片

 

5.newman使用方法

1)newman的安装:
npm install -g newman
2)检验是否安装成功:
newman -v
3)newman帮助:
newman -h
4)newman执行结果:

Postman实现接口自动化测试_第10张图片

 

Postman实现接口自动化测试_第11张图片

 

5)newman执行并生成报告:
1.json文件报告
newman run test.postman_collection.json -e test.postman_environment.json -n1 --reporters cli,json --reporter-json-export test_report.json
2.html文件报告
1.安装html报告模板:
npm install -g newman-reporter-html
newman run test.postman_collection.json -e test.postman_environment.json -n1 --reporters html --reporter-html-export test_report.html

6.jekins集成

1)新建项目
1.构建任务名;
2.选择任务方式;

Postman实现接口自动化测试_第12张图片

2).jekins定时执行脚本;
3).触发执行条件
1.其他工程构建后触发;
2.定时构建;

Postman实现接口自动化测试_第13张图片

 


4).关注构建结果

三:总结

Postman实现接口自动化测试_第14张图片

 

好了  这次就学习到这了哦! 

还想学习的话,记得收藏及关注哦


感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

① 2000多本软件测试电子书(主流和经典的书籍应该都有了)

② 软件测试/自动化测试标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python编程语言、API接口自动化测试、web自动化测试、App自动化测试(适合小白学习)


————————————————

 

 

⑤ Python学习路线图(告别不入流的学习) 

 在我的QQ技术交流群里(技术交流和资源共享,广告进来腿给你打断)

可以自助拿走,群号768747503备注(csdn999)群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。


 

你可能感兴趣的:(软件测试,自动化测试,接口测试,postman,功能测试,测试用例,集成测试)