`Postman`的断言实际上就是往全局对象`tests`添加键值对。它的`key`会显示在报告界面上,`value`是可以解析为`boolean`的表达式。如果得出的值是true,在报告里就会显示为成功,`false`失败。
## **创建断言**
![](https://img.kancloud.cn/a1/c9/a1c9fcf109af227c8a758744b455f7eb_1920x1030.png)
## **语法**
* 环境变量
~~~
// 获取
pm.environment.get("variable_key");
// 设置
pm.environment.set("variable_key", "variable_value");
// 清除
pm.environment.unset("variable_key");
~~~
* 集合变量
~~~
// 获取
pm.collectionVariables.get("variable_key");
// 设置
pm.collectionVariables.set("variable_key", "variable_value")
// 清除
pm.collectionVariables.unset("variable_key")
~~~
* 全局变量
~~~
// 获取
pm.globals.get("variable_key");
// 设置
pm.globals.set("variable_key", "variable_value");
// 清除
pm.globals.unset("variable_key");
~~~
* 在环境变量和集合变量和全局变量中搜索变量
~~~
pm.variables.get("variable_key");
~~~
* 检查响应主体是否包含字符串
~~~
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
~~~
* 检查响应主体是否等于字符串
~~~
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
~~~
* 检查JSON值
~~~
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
~~~
* 存在Content-Type标头
~~~
pm.test("Content-Type header is present", function () {
pm.response.to.have.header("Content-Type");
});
~~~
* 响应时间小于200ms
~~~
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
~~~
* 状态码为200
~~~
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
~~~
* 成功的POST请求状态代码
~~~
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
~~~
- Chrome
- Zend Studio
- 首选项
- svn
- 安装插件
- Problem Occurred
- 显示隐藏文件
- 启动显示Launcher
- 全文检索
- 误删文件的恢复
- 代码的比较和恢复
- 内置函数没有提示
- 显示导航栏
- 导入已存在项目
- 导入Composer项目
- PLSQL Developer
- 基本操作
- SQL Window的使用
- 导出表结构和数据
- 导入SQL文件
- Navicat
- 工作界面
- 逆向数据库到模型
- 命令行工具
- 导入SQL文件
- Postman
- 发送请求
- GET
- POST
- 变量
- 环境变量
- 集合变量
- 全局变量
- 内置变量
- pm对象
- 脚本测试
- Pre-request
- Tests
- Runner
- 实例
- token
- cookie
- 使用数据文件
- 导入chrome请求
- 导入Fiddler请求
- 下载文件
- 控制台的使用
- 生成代码片段
- Fiddler
- 界面
- 配置
- Android7.0+配置
- Inspectors
- Filters
- AutoResponder
- Composer
- FiddlerScript
- Breakpoints
- 实例
- 微信登录
- 微信文章
- 微信视频号
- 微信小程序
- HTTP Debugger Pro
- 界面
- Charles
- 界面
- 基础配置
- Breakpoints
- Rewrite
- Map Local
- Map Remote
- JMeter
- BurpSuite
- SVN
- 菜单
- 创建svn仓库
- SVN本地导入和导出
- 创建工作副本
- 查看连接错误信息
- 文件颜色标志
- 清除缓存信息
- Git
- Git简介
- 配置
- 工作区域
- 工作流程
- 创建仓库
- 微信小程序
- Composer
- create-project
- install
- require
- update
- remove
- search
- show
- npm
- info
- search
- list
- install
- uninstall
- update
- run
- 其它命令
- VSCode
- 常用插件
- Other
- Notepad++配置
- Hbuilder配置
- Navicat配置
- Win10配置
- Proxifier
- C盘爆满