【Postman学习】Postman断言

Postamn断言

复制代码
 1 断言内容详解
 2 postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。
 6 1.设置环境变量--Setting an environment variable 
 7 postman.setEnvironmentVariable("key", "value");
 8 
 9 2.设置全局变量--Set a global variable
10 postman.setGlobalVariable("key", "value");
11 
12 3.检查响应中包含string--Check if response body contains a string
13 tests["Body matches string"] = responseBody.has("string_you_want_to_search");
14 
15 4.转化XML格式的响应成JSON对象---Convert XML body to a JSON object
16 var jsonObject = xml2Json(responseBody);
17 
18 5.检查响应body中等于指定string--Check if response body is equal to a string
19  tests["Body is correct"] = responseBody === "response_body_string";
20 
21 6.检查JSON某字段值--Check for a JSON value
22 var data = JSON.parse(responseBody);
23 
24 tests["Your test name"] = data.value === 100;
25 
26 7.检查Content-Type是否包含在header返回(大小写不敏感)--Content-Type is present (Case-insensitive checking)
27  tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
28 
29 8.检查Content-Type是否包含在header返回(大小写敏感)--Content-Type is present (Case-sensitive)
30  tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
31 
32 9.检查请求耗时时间小于200ms--Response time is less than 200ms
33 tests["Response time is less than 200ms"] = responseTime < 200;
34 
35 10.检查Status code为200--Status code is 200
36 tests["Status code is 200"] = responseCode.code === 200;
37 
38 11.检查Code name是否指定string--Code name contains a string
39  tests["Status code name has string"] = responseCode.name.has("Created");
40 
41 12.检查成功post的请求status code--Succesful POST request status code
42 tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
复制代码

 

posted @   gtea  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示