当前位置:网站首页>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断言工作原理
边栏推荐
- 比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
- P2022 interesting numbers (binary & digit DP)
- Introduction to hashtable
- 2328. 网格图中递增路径的数目(记忆化搜索)
- Dynamic programming (tree DP)
- NPM command -- install dependent packages -- Usage / explanation
- [HBZ share] reasons for slow addition and deletion of ArrayList and fast query
- coreldraw2022新版本新功能介绍cdr2022
- Can CDC pull the Oracle table in full
- word封面下划线
猜你喜欢
How does computer nail adjust sound
CADD course learning (8) -- virtual screening of Compound Library
[tomato assistant installation]
The value of two date types is subtracted and converted to seconds
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
Recommendation | recommendation of 9 psychotherapy books
Easyrecovery靠谱不收费的数据恢复电脑软件
11. Intranet penetration and automatic refresh
Fedora/REHL 安装 semanage
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
随机推荐
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
ISP学习(2)
Recommendation | recommendation of 9 psychotherapy books
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
CertBot 更新证书失败解决
【HBZ分享】ArrayList的增删慢查询快的原因
Redis 排查大 key 的4種方法,優化必備
Selection of slow motion function
coreldraw2022新版本新功能介绍cdr2022
Ue5 small knowledge freezerendering view rendered objects in the cone
牛顿插值法
二叉树基本知识和例题
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
flink sql 能同时读多个topic吗。with里怎么写
What should the project manager do if there is something wrong with team collaboration?
MySQL reported an error datetime (0) null
Database - MySQL storage engine (deadlock)
Introduction of several RS485 isolated communication schemes
Visio draws Tai Chi