postman tests实例记录
来源:互联网 发布:知乎 宋徽宗 编辑:程序博客网 时间:2024/06/05 04:29
这段时间准备测试api接口,postman这个工具很是方便,特别是里面的tests的javascript脚本。
记录一下测试接口常用的tests验证的实例。
1.设置环境变量
postman.setEnvironmentVariable("key", "value");
2.将嵌套独享设置为环境变量
var array = [1, 2, 3, 4];postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };postman.setEnvironmentVariable("obj", JSON.stringify(obj));
3.获取环境变量
postman.getEnvironmentVariable("key");获取一个环境变量(其值是一个字符串对象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.var array = JSON.parse(postman.getEnvironmentVariable("array"));var obj = JSON.parse(postman.getEnvironmentVariable("obj"));
4.清除一个环境变量
postman.clearEnvironmentVariable("key");
5.设置一个全局变量
postman.setGlobalVariable("key", "value");
6.获取全局变量
postman.getGlobalVariable("key");7.清除全局变量
postman.clearGlobalVariable("key");8.检查response是否包含一个字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");9.将xml体转换为JSON对象
var jsonObject = xml2Json(responseBody);10.检查response是否等于一个字符串
tests["Body is correct"] = responseBody === "response_body_string";11.检查JSON值
var data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;12.内容类型存在(不区分大小写的检查)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.13.内容类型存在(区分大小写)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");14.响应时间小于200ms
tests["Response time is less than 200ms"] = responseTime < 200;
15.响应时间在一个特定的范围内(包括下限和上限)
tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001); // _ is the inbuilt Lodash v3.10.1 object, documented at https://lodash.com/docs/3.10.116.状态码是200
tests["Status code is 200"] = responseCode.code === 200;17.代码包含一个字符串
tests["Status code name has string"] = responseCode.name.has("Created");18.成功的POST请求状态码
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;19.将TinyValidator用于JSON数据
var schema = { "items": { "type": "boolean" }};var data1 = [true, false];var data2 = [true, 123];tests["Valid Data1"] = tv4.validate(data1, schema);tests["Valid Data2"] = tv4.validate(data2, schema);console.log("Validation failed: ", tv4.error);
20.解码base64编码数据
var intermediate,base64Content, // assume this has a base64 encoded valuerawContent = base64Content.slice('data:application/octet-stream;base64,'.length);intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-jstests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate); // a check for non-emptiness
官方文档英文版传送门:
https://www.getpostman.com/docs/postman/scripts/test_examples
阅读全文
0 0
- postman tests实例记录
- POSTMAN tests 简单记录
- Postman request test 实例
- Postman工具——Pre-Request Script、Tests
- Postman工具——Pre-Request Script、Tests
- tests
- Postman Tests中设置环境变量,结果转json数值失去精度
- POSTman chrome插件使用学习记录
- Postman
- PostMan
- Postman
- postman
- postman
- Postman
- PostMan
- postman
- postman
- PostMan
- bootstraptable表格相关总结
- APPlication
- Hadoop集群动态增删节点及备份系数调整
- json解析并存放到sd卡文件中
- ThreadLocal线程安全
- postman tests实例记录
- Kotlin 学习之准备
- 文本搜索过程以及倒排搜索
- DNS的解析杂谈,以及gethostbyname的弊端
- Android 复杂的多类型列表视图新写法:MultiType 3.0
- 行为型模式总结篇(2)
- java根据时间date获取该时间距离现在的时间间隔
- 快速幂
- codeforces 853c (关于矩形的主席树)