Postman 常用断言脚本合集
Postman 全套可直接复制断言脚本合集
前置通用代码(所有脚本开头统一获取返回JSON)
// 固定放在Tests最顶部,统一接收响应jsonletres=pm.response.json();一、基础状态码断言
// 1. 校验接口成功200pm.test("接口响应状态码为200",function(){pm.expect(pm.response.code).to.eql(200);});// 2. 校验创建资源成功201pm.test("新增资源返回201",function(){pm.expect(pm.response.code).to.eql(201);});// 3. 校验无权限401/禁止访问403pm.test("未登录返回401",function(){pm.expect(pm.response.code).to.eql(401);});pm.test("无操作权限返回403",function(){pm.expect(pm.response.code).to.eql(403);});// 4. 校验参数错误400pm.test("非法参数返回400",function(){pm.expect(pm.response.code).to.eql(400);});// 5. 校验服务异常500pm.test("服务器异常返回500",function(){pm.expect(pm.response.code).to.eql(500);});二、业务码、返回提示文案断言
// 1. 校验业务成功码(通用后端规范 code=200 / code=0)pm.test("业务操作成功,code=200",function(){pm.expect(res.code).to.eql(200);});pm.test("业务操作成功,code=0",function(){pm.expect(res.code).to.eql(0);});// 2. 校验失败提示信息pm.test("参数错误提示文案正确",function(){pm.expect(res.msg).to.include("参数不能为空");});// 3. 校验返回msg不为空pm.test("返回提示信息非空",function(){pm.expect(res.msg).not.to.be.empty;});三、JSON 多层级字段校验(最常用)
场景1:提取token并校验(登录接口专用)
pm.test("返回token,且不为空字符串",function(){// 校验字段存在、类型为字符串、非空pm.expect(res.data.token).to.be.a("string");pm.expect(res.data.token).not.to.be.empty;});// 同时存入环境变量,实现接口关联vartoken=res.data.token;pm.environment.set("token",token);console.log("提取到的token:",token);// 控制台打印调试场景2:多层嵌套字段校验
返回示例:{"code":200,"data":{"user":{"id":1001,"name":"测试用户"}}}
pm.test("返回用户ID为数字",function(){pm.expect(res.data.user.id).to.be.a("number");pm.expect(res.data.user.id).greaterThan(0);});pm.test("用户名不为空",function(){pm.expect(res.data.user.name).not.empty;});场景3:数组列表校验(分页列表接口)
返回示例:{"code":200,"data":{"list":[{"id":1},{"id":2}],"total":2}}
pm.test("列表是数组格式",function(){pm.expect(res.data.list).to.be.an("array");});pm.test("列表数据条数大于0",function(){pm.expect(res.data.list.length).greaterThan(0);});pm.test("分页总条数正确",function(){pm.expect(res.data.total).to.eql(2);});// 提取列表第一条id存入环境变量(用于后续删除/编辑接口)letfirstId=res.data.list[0].id;pm.environment.set("targetId",firstId);四、响应头断言(鉴权、缓存、跨域校验)
// 校验响应头携带Tokenpm.test("响应头返回Authorization",function(){pm.expect(pm.response.headers.get("Authorization")).to.exist;});// 校验跨域允许所有域名pm.test("允许跨域访问",function(){pm.expect(pm.response.headers.get("Access-Control-Allow-Origin")).to.eql("*");});// 校验返回Content-Type为jsonpm.test("返回数据格式为JSON",function(){pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");});五、响应时间断言(简易性能测试)
// 普通接口响应小于800mspm.test("接口响应耗时 < 800ms",function(){pm.expect(pm.response.responseTime).to.be.below(800);});// 分页列表慢接口放宽到1500mspm.test("列表接口响应耗时 < 1500ms",function(){pm.expect(pm.response.responseTime).to.be.below(1500);});六、反向断言(异常场景用例)
// 字段不等于某个值pm.test("错误码不能是200",function(){pm.expect(res.code).not.to.eql(200);});// 字段不存在pm.test("正常接口不返回error字段",function(){pm.expect(res).not.to.have.property("error");});// 数组为空(查询无数据场景)pm.test("暂无数据,列表为空",function(){pm.expect(res.data.list.length).to.eql(0);});七、数据驱动/通用变量提取模板
1. 存入环境变量(仅当前环境生效,自动化首选)
// 提取字符串tokenpm.environment.set("token",res.data.token);// 提取数字IDpm.environment.set("orderId",res.data.order.id);// 提取文本pm.environment.set("username",res.data.user.name);2. 存入全局变量(所有环境共享)
pm.globals.set("globalToken",res.data.token);3. 清除变量(后置清理脚本)
// 用完删除环境变量pm.environment.unset("token");pm.environment.unset("orderId");八、完整可直接复制的登录接口整套Tests脚本
// 1. 获取返回JSONletres=pm.response.json();// 2. 状态码断言pm.test("登录接口返回200",function(){pm.expect(pm.response.code).to.eql(200);});// 3. 业务码断言pm.test("登录成功code=200",function(){pm.expect(res.code).to.eql(200);});// 4. 校验token存在pm.test("返回有效token",function(){pm.expect(res.data.token).to.be.a("string");pm.expect(res.data.token).length.greaterThan(10);});// 5. 提取token存入环境变量,供后续接口鉴权使用vartoken=res.data.token;pm.environment.set("token",token);// 6. 响应时间校验pm.test("登录接口响应小于500ms",function(){pm.expect(pm.response.responseTime).to.be.below(500);});// 7. 打印日志调试console.log("登录成功,已存入token:",token);九、下游接口引用变量示例
Headers 鉴权头
Authorization: Bearer {{token}}Body JSON参数
{"id":"{{targetId}}","name":"测试名称"}补充使用小技巧
- 脚本全部粘贴到请求右侧
Tests标签,发送请求自动执行; - 结果在
Test Results面板查看,绿色=通过,红色=失败; console.log()打印内容在底部Console窗口查看,调试变量;- 数据驱动Runner使用CSV时,参数直接写
{{列名}}即可读取每行数据。
