postman 断言

postman断言

 常用的断言
  Status code:code is 200 检查返回的状态码是否为200
  Response body:Contains string 检查响应中包含指定字符串
  Response body:JSON value check 检查响应中JSON的值
  Response body:is equal to a string 检查响应值是否等于字符串
  Response headers:Content-Type header check 检查是否包含响应头Content-Type
  Response time is less than 200ms 检查请求耗时小于200ms
 断言中获取自定义参数(全局变量)的方式
  pm.globals.get('times')
  globals['times']
  globals.times
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 状态断言
// 判断请求返回的状态为200200就是正常
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
 
// 业务断言
// 判断请求返回中其它字段的值(或状态)
pm.test("检查响应中是否包含...", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
// 配合全局变量,第一种
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("..."+ pm.globals.get("times"));
});
// 第二种
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("..."+ globals["times"]);
});
// 第三种
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("..."+ globals.times);
});
 
// 获取json数据,并校对返回值的正确性
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
 设置全局断言
0
  设置全局断言后,断言会在项目内所有接口测试时执行
posted @   乌醍  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示