Postman 如何设置断言?

简介: 大家好,我是阿萨。今天是Postman 21天打卡的第8天。昨天学习了如何使用Postman 做好Cookie管理以及授权?大家都掌握了设置授权的方法。

对于测试用例来说,断言是最重要的,通过断言我们来判断测试通过没有。那么如何写好API测试的断言呢?


断言书写的位置见下图。


a23a279e9aff4003b0500e1a5e6be0a5_tplv-k3u1fbpfcp-zoom-in-crop-mark_4536_0_0_0.jpg


今天我们列举下Postman API常见断言写法。


Postman 使用chai 这个语言去写的脚本。

 

chai 写断言分2种:


  1. 1.BDD


  1. 2.TDD


Postman 采用的是BDD的写法。


Postman BDD 的常见写法有:


    pm.response.to.not.be.error;    pm.response.to.have.jsonBody("");    pm.response.to.not.have.jsonBody("error");
    pm.response.to.be.ok;    pm.response.to.be.withBody;    pm.response.to.be.json;
    var jsonData = pm.response.json();    pm.expect(jsonData.value).to.eql(100);    pm.expect(jsonData).to.not.have.property('Sarah-Postman-demo');
    var expectedJson={a:1};    pm.expect(jsonData).to.deep.equal(expectedJson);    pm.expect(jsonData).to.have.nested.property('Sarah-Postman-demo');
    pm.expect(jsonData).to.nested.include('Sarah-Postman-demo');
    pm.expect(jsonData).to.have.ordered.members(expectedJson).but.not.have.ordered.members([2,1]);
    pm.expect(jsonData.value).to.have.any.keys('chart','Sarah-Postman-demo');
    pm.expect('chart').to.exist;    pm.response.to.have.body("Sarah-Postman-demo");    pm.response.to.have.status(200);    pm.expect(pm.response.text()).to.include("Sarah-Postman-demo");
    pm.response.to.have.header("Content-Type");         pm.expect(pm.response.responseTime).to.be.below(200);
     pm.expect(pm.response.code).to.be.oneOf([201, 202]);
    pm.response.to.have.status("Sarah-Postman-demo");


断言可以写在一个function里也可以写在多个functon里。 断言可以使用自己熟悉的javascript 语言去写。另外除了断言,前置条件中也可以写javascript。 后续会介绍javascript如何写前置条件。


你学会了吗?


如果你也想学习如何使用Postman 工具,欢迎跟着阿萨 一起完成21天打卡学习。



相关文章
|
测试技术 API
用Postman 如何设置断言?
用Postman 如何设置断言?
311 0
Postman——如何获取当前时间并设置为全局变量
Postman——如何获取当前时间并设置为全局变量
748 4
|
API 数据安全/隐私保护 网络架构
【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
399 0
Postman - 设置全局请求 header 信息
Postman - 设置全局请求 header 信息
4627 0
Postman - 设置全局请求 header 信息
Postman设置全局变量
Postman设置全局变量
452 0
|
前端开发
Postman接口Mock Server服务器设置
Postman接口Mock Server服务器设置,适用场景:后端的接口还没有开发完成,前端的业务需要调用后端的接口;当前端需要什么数据,mock模拟的结果直接给什么数据即可。
308 0
Postman接口Mock Server服务器设置
|
JSON JavaScript 测试技术
|
Web App开发 JSON 缓存
【Postman】设置内容
【Postman】设置内容
297 0
【Postman】设置内容
|
Web App开发 API
Postman教程-通过设置代理/拦截器捕获请求信息
通过Postman捕获到的请求信息,默认不会显示请求响应结果,只会包含请求的相关参数信息。
Postman教程-通过设置代理/拦截器捕获请求信息
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。