当前位置:网站首页>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 ——> 预期结果!
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3wT6V0cS-1656839916613)(接口测试-第03天-课堂笔记.assets/image-20210709103914593.png)]](/img/26/38bc54b39096c91ee498cd7dfb5212.png)
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断言工作原理

边栏推荐
- Microservice resource address
- cdc 能全量拉去oracle 表嘛
- The value of two date types is subtracted and converted to seconds
- 2327. Number of people who know secrets (recursive)
- Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
- Recommendation | recommendation of 9 psychotherapy books
- NPM command -- install dependent packages -- Usage / explanation
- Redis 排查大 key 的4種方法,優化必備
- VPP performance test
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
猜你喜欢

Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system

How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."

Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码

Bill Gates posted his 18-year-old resume and expected an annual salary of $12000 48 years ago

The value of two date types is subtracted and converted to seconds

Use sentinel to interface locally

Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps

Canal synchronizes MySQL data changes to Kafka (CentOS deployment)

Is the mode of education together - on campus + off campus reliable

canal同步mysql数据变化到kafka(centos部署)
随机推荐
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Embedded development program framework
我想问一下 按照现在mysql-cdc的设计,全量阶段,如果某一个chunk的binlog回填阶段,
也算是學習中的小總結
Lombok principle and the pit of ⽤ @data and @builder at the same time
I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
web工程导入了mysql驱动jar包却无法加载到驱动的问题
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
MySQL reported an error datetime (0) null
牛顿插值法
Redis 排查大 key 的4种方法,优化必备
Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming
Fedora/rehl installation semanage
Mixed development of QML and QWidget (preliminary exploration)
[try to hack] John hash cracking tool
项目经理,你会画原型嘛?项目经理需要做产品设计了?
Fedora/REHL 安装 semanage
VPP性能测试