
Postman 全套可直接复制断言脚本合集前置通用代码所有脚本开头统一获取返回JSON// 固定放在Tests最顶部统一接收响应jsonletrespm.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. 校验业务成功码通用后端规范 code200 / code0pm.test(业务操作成功code200,function(){pm.expect(res.code).to.eql(200);});pm.test(业务操作成功code0,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;});// 同时存入环境变量实现接口关联vartokenres.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存入环境变量用于后续删除/编辑接口letfirstIdres.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. 获取返回JSONletrespm.response.json();// 2. 状态码断言pm.test(登录接口返回200,function(){pm.expect(pm.response.code).to.eql(200);});// 3. 业务码断言pm.test(登录成功code200,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存入环境变量供后续接口鉴权使用vartokenres.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时参数直接写{{列名}}即可读取每行数据。