Postman接口测试实战

本文详细介绍了Postman的接口关联、动态参数、断言、加解密、持续集成以及Mock测试的实战技巧,涵盖了JSON提取、正则表达式、系统变量、自定义参数、状态码验证、加密解密和自动化测试等内容,提升开发者API测试效率。
摘要由CSDN通过智能技术生成

Postman接口测试实战

一、Postman的接口关联

方式一 JSON提取器

//打印用于调试
console.log(responseBody);
//把返回的字符串转换成JSON对象。
var baili = JSON.parse(responseBody);
//取值
console.log(baili.access_token);

方式二 正则表达式提取器

//通过正则匹配值,match匹配
var res= responseBody.match(new RegExp('"token":"(.*?)"'));
console.log(res[1]);
//设置成全局变量
pm.globals.set("token", datas[1]);

二、Postman的动态参数

方式一 系统自带的动态参数

{{$timestamp}} //动态时间戳
{{$randomInt}} //动态0-1000的整形
{{$guid}} //动态的guid字符串

方式二 自定义的动态参数

//自定义动态参数生产随机数
var times = Date.now();
pm.globals.set("times", times);
//定时五秒
const sleep = (milliseconds) => {
    const start = Date.now();
    while (Date.now() <= start + milliseconds) {
    }
};
sleep(5000);

三、Postman的断言

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
// ps:  1.在断言中不能够使用{{}}的方式取全局变量。
//      2.一般通过:pm.globals.get("times") 取全局变量的值

四、Postman处理加解密

//Base64位加密

//把需要加密的值转换成utf-8的编码格式3
var us = CryptoJS.enc.Utf8.parse("admin");
var pw = CryptoJS.enc.Utf8.parse("123");

//对转换后的值做Base64加密
var bs64_us = CryptoJS.enc.Base64.stringify(us);
var bs64_pw = CryptoJS.enc.Base64.stringify(pw);
//设置为全局变量
pm.globals.set("bs64_us", bs64_us.toString().toUpperCase());
pm.globals.set("bs64_pw", bs64_pw.toString().toUpperCase());

//Base64位解密
var intermediate = CryptoJS.enc.Base64.parse(responseBody);
var base64Content = intermediate.toString(CryptoJS.enc.Utf8);
//对解密后的数据进⾏提取json部分
var jsonValue = base64Content.substring(base64Content.indexOf("{"), base64Content.lastIndexOf("}") + 1);
console.log(jsonValue);

五、Postman持续集成

# 1.安装Node.js

# 2.安装newman
npm install -g newman

# 3.导出postman的脚本并执行 导出:用例,环境,全局变量。
newman run "e:\newmans\yongli.json" -e "e:\newmans\huanjing.json" -g "e:\newmans\quanju.json" -r cli,html,json,junit --reporter-html-export "e:\newmans\report.html"

六、Postman Mock测试

在前后端开发过程中,需求过来,但是后端在开发的进程中,这时候前端想调用接口无法实现,因此需要用模拟服务器模拟出所要开发接口的属性(包括返回值,请求参数等),如果每个接口都要等后端开发完成再进行测试会很浪费时间,因此使用模拟接口来测试前端代码的功能,极大的缩短了等待时间,到后期后端全部开发出来接口再配合联调测试即可。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzixiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值