02 接口自动化-精通Postman之接口关联,动态参数,断言以及Postman+Newman+Jenkins持续集成

一、接口关联,接口依赖

下一个接口的参数是使用的上一个接口的返回值?
1.JSON提取器(都是从返回值里面提取)

//javascript脚本,var定义变量
//打印responseBody返回值
console.log(responseBody)
//使用json提取器把responseBody返回值转化成一个字典。
var jd = JSON.parse(responseBody)
//提取access_token,并且设置为全局变量(就是在任何接口请求都可以访问的变量)
pm.globals.set("access_token",jd.access_token);

取得全局变量:{{access_token}}

2.正则表达式提取器(都是从返回值里面提取)

var token = responseBody.match(new RegExp('"access_token":"(.*?)"'));
pm.globals.set("access_token",token[1]);

3.从响应头里面中去提取

//从响应头里面提取变量
var types = postman.getResponseHeader("Content‐Type")
console.log(types)

4.从Cookie里面中去提取

//从Cookie里面提取变量
var csrf_token = postman.getResponseCookie('csrf_token');
console.log(csrf_token.value)

二、Postman的动态参数

接口测试中常常会出现接口的参数不能写死,必须使用随机数来实现。
1.内置的动态参数

  • {{$timestamp}} 时间戳
  • {{$randomInt}} 随机的0-1000的整数
  • {{$guid}} 随机的很长的字符串

2.自定义动态参数(重点)

//自定义的时间戳
var times = Date.now();
pm.globals.set("times",times);
//让接口请求停留3秒(3秒灌水机制),time.sleep(3)
const sleep = (milliseconds) => {
const start = Date.now();
while (Date.now() <= start + milliseconds) {}
};
sleep(3000);

三、Postman的全局变量和环境变量

全局变量: 就是在所有接口请求里面都可以访问的变量
环境变量: 就是全局变量(开发环境,测试环境,线上环境)

四、Postman的断言

断言内容:响应状态断言+业务断言

  • 断言返回码为200
  • 断言返回结果中包含指定的字符串
  • 断言并检查返回的JSON数据
  • 断言返回的值等于一个字符串
  • 断言响应头包含Content-type
  • 断言响应时间少于200M

1.断言返回码为200

pm.test("Response status code is 200", function () {
    pm.response.to.have.status(200);
});

2.断言返回结果中包含指定的字符串

pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

3.断言并检查返回的JSON数据

pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});

4.断言返回的值等于一个字符串

pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});

5.断言响应头包含Content‐type

pm.test("Content‐Type is present", function () {
pm.response.to.have.header("Content‐Type");
});

6. 断言响应时间少于200MS

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

!!!特别注意:

  • 1.postman内置的动态参数无法做断言。所以必须使用自定义的动态参数。
  • 2.在tests里面不能使用{{}}的方法取全局变量,必须使用以下方式:
pm.globals.get("times1")
globals['times1']
globals.times1

五、Postman+newman+jenkins实现自动生成报告并持续集成。

Postman是接口测试而生
Newman是为Postman而生(新男人)

1、安装
  • 安装Node.js
    下载地址:https://nodejs.org/zh-cn/download 双击安装
    验证:打开cmd,输入node出现>说明安装成功。
  • 安装npm
    打开cmd输入:npm install --global --production windows-build-tools
    等待安装完成即可。
  • 安装newman
    打开cmd输入:npm install -g newman
    验证:打开cmd输入newman -v 出现版本信息说明成功。
2、导出postman的测试用例,环境变量,全局变量

newman run "e:\\yongli.json" -e "e:\\huanjing.json" -g "e:\\quanju.json" -r cli,html,json,junit --reporter-html-export "e:\\report.html"
-e 环境变量
-g 全局变量
-r cli,html,json,junit --reporter-html-export 测试报告输出的路径
注:生成html报告,需要先通过 npm install newman-reporter-html 命令安装 newman-reporter
-html

3、和jenkins持续集成

先安装:jenkins 安装教程
安装后: 请参考 postman与jenkins集成官方教程

  • 构建:执行window批处理命令

  • 构建后:Publish HTML Reports

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值