postman 常用参数例子

复制代码
文档路径:https://learning.postman.com/docs/getting-started/navigating-postman/

常用tests用法如下:
1.检查response body中是否包含某个string tests["Body matches string"] = responseBody.has("string_you_want_to_search"); 注意:"Body matches string" 需唯一。 2.检测JSON中的某个值是否等于预期的值 var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100; JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。 如:检查json中某个数组元素的个数(这里检测programs的长度) var data = JSON.parse(responseBody); tests["program's lenght"] = data.programs.length === 5; 3.检查response body是否与某个string相等 4.转换XML body为JSON对象 var jsonObject = xml2Json(responseBody); tests["Body is correct"] = responseBody === "response_body_string"; 5.测试response Headers中的某个元素是否存在(如:Content-Type) tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //getResponseHeader()方法会返回header的值,如果该值存在 或者: tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type"); 上面的方法,不区分大小写。下面的方法,要区分大小写。 6.验证Status code的值 tests["Status code is 200"] = responseCode.code === 200; 7.验证Response time是否小于某个值 tests["Response time is less than 200ms"] = responseTime < 200; 8.name是否包含某个值 tests["Status code name has string"] = responseCode.name.has("Created"); 9.POST 请求的状态响应码是否是某个值 tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202; 10.很小的JSON数据验证器 var schema = { "items": { "type": "boolean" } }; var data1 = [true, false]; var data2 = [true, 123]; console.log(tv4.error); tests["Valid Data1"] = tv4.validate(data1, schema); tests["Valid Data2"] = tv4.validate(data2, schema);
复制代码

 

posted on   陈惟鲜的博客  阅读(72)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 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
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示