postman中Test断言介绍

Test断言

  • 一,常用断言:
    • [1)Status code:Code is 200 检查返回的状态码是否为200](#1)Status code:Code is 200 检查返回的状态码是否为200)
    • [2)Response body:Contains string 检查响应中包括指定字符串](#2)Response body:Contains string 检查响应中包括指定字符串)
    • [3)Response body:Json value check 检查响应中其中json的值](#3)Response body:Json value check 检查响应中其中json的值)
    • [4)Response body:is equal to a string 检查响应等于一个字符串](#4)Response body:is equal to a string 检查响应等于一个字符串)
    • [5)Response headers:Content-Type...检查是否包含响应头Content-Type](#5)Response headers:Content-Type...检查是否包含响应头Content-Type)
    • [6)Response time is less than 200ms:检查请求耗时小于200ms](#6)Response time is less than 200ms:检查请求耗时小于200ms)
    • [7)Status code :Successfull POST request :判断post请求的状态码,是不是在201和202这两个之中](#7)Status code :Successfull POST request :判断post请求的状态码,是不是在201和202这两个之中)
  • 二,使用参数

一,常用断言:

前4种最常用

1)Status code:Code is 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.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("email");
});

3)Response body:Json value check 检查响应中其中json的值

// pm.test("Your test name", function () {
//     var jsonData = pm.response.json();
//     pm.expect(jsonData.value).to.eql(100);
// });
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.list[0]).to.eql("第一个数");
});

4)Response body:is equal to a string 检查响应等于一个字符串

response_body_string处填写的内容要和接口返回的内容完全一致才可以匹配成功

// pm.test("Body is correct", function () {
//     pm.response.to.have.body("response_body_string");
// });

5)Response headers:Content-Type...检查是否包含响应头Content-Type

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

6)Response time is less than 200ms:检查请求耗时小于200ms

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

7)Status code :Successfull POST request :判断post请求的状态码,是不是在201和202这两个之中

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});

二,使用参数

global全局参数

断言使用参数

Postman内置动态参数和自定义的动态参数以及断言方式

   {{$timestamp}}  生成当前时间的时间戳。

   {{$randomInt}}  生成0-1000之间的随机数

   {{$guid}}           生成速记GUID字符串
相关推荐
99乘法口诀万物皆可变5 小时前
ODX相关基础知识普及
测试工具·测试用例
今天好像不上班6 小时前
软件验证与确认实验二-单元测试
测试工具·算法
wdxylb1 天前
Pyhton爬虫使用Selenium实现浏览器自动化操作抓取网页
爬虫·selenium·测试工具
程序员陆通2 天前
如何使用ChatGPT API及Bito插件
开发语言·chatgpt·lua
码力码力我爱你2 天前
C++ WebDriver扩展
c++·selenium·测试工具
Play_Sai2 天前
【接口测试】任务2:商品分类接口
软件测试·接口测试·postman·技能竞赛·楚怡杯·jemter
谷隐凡二2 天前
windows环境下luarocks下载包的使用
开发语言·lua
土小帽软件测试3 天前
fiddler抓包16_自动重定向(mock数据)
测试工具·fiddler·抓包·软件测试学习
程序那点事儿3 天前
open-resty 服务安装kafka插件
linux·分布式·nginx·kafka·lua·运维开发·openresty
Kakaxiii3 天前
UMLS的api调用(Postman)
人工智能·深度学习·测试工具·自然语言处理·postman