当前位置:网站首页>Postman断言
Postman断言
2022-07-06 04:41:00 【炫酷的腿毛!】
Postman断言
Postman 断言简介
- postman 断言借助 JavaScript - js 语言编写代码,自动判断预期结果与实际结果是否一致。
- 断言 代码写在 Tests 的标签中。
Postman 常用断言
1. 断言响应状态码
Status code: Code is 200
- 在 Tests 标签中,选中 Status Code:code is 200, 生成对应代码
- 适当调整 test() 方法参数1,和 匿名函数中的 预期结果。
- 点击 send 按钮,发送请求,执行断言代码。
- 查看断言结果。
// 断言响应状态码 是否为 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm:代表 postman 的一个实例
test():是 pm实例的一个方法。有两个参数
参数1:在断言成功后,给出的文字提示。可以修改。"Status code is 200"
参数2:匿名函数。
pm.response.to.have.status(200);
// 意思:postman 的响应结果中应该包含状态码 200
200 ——> 预期结果!
2. 断言响应体是否包含某个字符串
Response body: Contains string
// 断言响应体包含指定字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm:postman的一个实例
test(): postman实例的方法,有两个参数
参1:断言后显示的文字提示信息,可改。
参2:匿名函数
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
// 意思:pm 期望 响应文本 中,包含 xxxx 字符串。
"string_you_want_to_search" ——> 预期结果。 可以修改
3. 断言响应体是否等于某个字符串(对象)
Response body: Is equal to a string
// 断言 响应体 等于某个字符串(对象)
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
pm.response.to.have.body("response_body_string");
// 意思是,pm 的 响应中应该有 响应体 xxx
"response_body_string" ——> 预期结果。 可以修改
4. 断言JSON数据
Response body: JSON value check
// 断言json的响应结果
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
var jsonData = pm.response.json();
// var jsonData: 用js语法定义一个变量。jsonData 就是变量名
// pm.response.json(); 代表响应的json结果
/* 举例:response.json(); { "success": true, "code": 10000, "message": "操作成功!", "data": "95c78d75-721c-40fb-b2d5-742fea42cbd5" } */
pm.expect(jsonData.value).to.eql(100);
// 意思:pm 预期 json结果 key对应的值 等于 xxx
// to.eql(100); 中的 100 代表预期结果。可以修改的。
/* 举例: jsonData.value 的 value: 取 :success、code、message、data */
示例:
// 断言json的响应结果-success的值为true
pm.test("断言响应结果success的值为true", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.success).to.eql(true);
});
pm.test("断言响应结果中code的值为10000", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.code).to.eql(10000);
});
pm.test("断言响应结果中message的值为 操作成功", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.message).to.eql("操作成功!");
});
5. 断言响应头
Response headers: Content-Type header check
// 断言响应头
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.response.to.have.header("Content-Type");
// pm 的响应 头中包含 Content-Type
// 示例:可以在 header 中,添加 响应头中的 key 对应的 value 判定。用 ,隔分。
// 断言响应头
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type", "application/json;charset=UTF-8");
});
Postman断言工作原理
边栏推荐
- Mysql database storage engine
- CADD course learning (8) -- virtual screening of Compound Library
- Basic explanation of turtle module - draw curve
- SharedPreferences 源码分析
- When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
- word封面下划线
- P3500 [POI2010]TES-Intelligence Test(二分&离线)
- Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
- [network] channel attention network and spatial attention network
- VPP性能测试
猜你喜欢
RTP gb28181 document testing tool
The value of two date types is subtracted and converted to seconds
Easyrecovery靠谱不收费的数据恢复电脑软件
View 工作流程
11. Intranet penetration and automatic refresh
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Dry goods collection | Vulkan game engine video tutorial
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Lombok principle and the pit of ⽤ @data and @builder at the same time
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
随机推荐
CertBot 更新证书失败解决
cdc 能全量拉去oracle 表嘛
ue5 小知识 FreezeRendering 查看视锥内渲染的物体
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Complete list of common functions of turtle module
[detailed steps of FreeRTOS shift value for the first time]
[Zhao Yuqiang] deploy kubernetes cluster with binary package
2328. Number of incremental paths in the grid graph (memory search)
动态规划(树形dp)
Dynamic programming (tree DP)
P2102 floor tile laying (DFS & greed)
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
也算是学习中的小总结
tengine 内核参数
[05-1, 05-02, 05-03] network protocol
Scala function advanced
Introduction of several RS485 isolated communication schemes
C. The Third Problem(找规律)
Certbot failed to update certificate solution
【HBZ分享】云数据库如何定位慢查询