一:Postman安装

1. 环境搭建:

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

二:Postman页面介绍

postman 前处理python脚本 python postman脚本自动化_postman 前处理python脚本

 

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 前处理python脚本 python postman脚本自动化_postman_02

 

postman 前处理python脚本 python postman脚本自动化_postman_03

 

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

postman 前处理python脚本 python postman脚本自动化_postman 前处理python脚本_04

 

4.变量

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

postman 前处理python脚本 python postman脚本自动化_测试用例_05

 

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

postman 前处理python脚本 python postman脚本自动化_测试用例_06

 

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

postman 前处理python脚本 python postman脚本自动化_postman_07

 

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

postman 前处理python脚本 python postman脚本自动化_postman_08

 

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

5.脚本(Script)

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

postman 前处理python脚本 python postman脚本自动化_集成测试_09

 

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 前处理python脚本 python postman脚本自动化_功能测试_10

 

5.newman使用方法

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

postman 前处理python脚本 python postman脚本自动化_功能测试_11

 

postman 前处理python脚本 python postman脚本自动化_集成测试_12

 

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 前处理python脚本 python postman脚本自动化_postman_13

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

postman 前处理python脚本 python postman脚本自动化_postman 前处理python脚本_14

 

4).关注构建结果

三:总结

postman 前处理python脚本 python postman脚本自动化_postman 前处理python脚本_15

 

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