postman使用-第七关 设置断点

当一个接口的请求返回结果后,判断接口是否符合预期,可以在 postman 里面的 Tests 写脚本断言符合结果符合预期。
Tests 是接口返回 response 之后的脚本操作,可以使用 JavaScript 为 Postman API 请求编写 Tests 脚本。
Tests编写
这是个请求接口
在这里插入图片描述这个为返回json

{
    "code": 0,
    "msg": "login success!",
    "username": "test",
    "token": "6fe731530358967e7ff9c9bb8c31ab5c8e373ff4"
}

可以校验返回的 body 是 json 格式

pm.test("response must be valid and have a body", function () {
     pm.response.to.be.ok;
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});

在这里插入图片描述点击send之后,运行后可以看到接口返回TestResults位置显示PASS,说明此校验通过

上面是直接pm.response.to.be方式对response对象校验的,也可以用pm.expect(actual_result).to 方式对提取的返回结果校验

// 校验code为0
pm.test("response code must to be 0", function () {
    pm.expect(pm.response.json().code).to.equal(0);
});

//校验msg 为 login success!
pm.test("response msg must to be login success!", function () {
    pm.expect(pm.response.json().msg).to.equal("login success!");
});

//校验token 长度为40位
pm.test("response token length must to be 40", function () {
    pm.expect(pm.response.json().token).to.lengthOf(40);
});

在这里插入图片描述
校验状态码和返回头部

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

在这里插入图片描述
校验 Content-Type 在返回头部

pm.test("Content-Type header is present", () => {
  pm.response.to.have.header("Content-Type");
});

校验返回的头部Content-Type 值为 application/json

pm.test("Content-Type header is application/json", () => {
  pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
});

如果我前面登陆的body参数引用了环境变量username

pm.test("Response property matches environment variable", function () {
  pm.expect(pm.response.json().username).to.eql(pm.environment.get("username"));
});

在这里插入图片描述点击右侧可以快捷生成
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值