- 安装与更新
- 发送第一个请求
- 创建第一个集合(collections)
- 界面布局
- 帐号
- 同步
- 设置
- 请求(Request)
- 响应(Response)
- 历史记录(History)
- API 请求疑难解答
- 调试和日志
- 授权(Authorization)
- Cookies
- 证书(Certificates)
- 捕获 HTTP 请求(Capturing HTTP requests)
- Interceptor 扩展
- 生成代码段(Generate code snippets)
- 发出 SOAP 请求
- 创建集合(Creating collections)
- 分享集合(Sharing collections)
- 管理集合(Managing collections)
- 使用 Markdown 添加描述
- 示例(Examples)
- 数据格式
- 脚本简介
- 预请求脚本(Pre-request scripts)
- 测试脚本
- 测试示例(Test examples)
- 分支和循环(Branching and looping)
- 沙盒(Sandbox)
- 变量(Variables)
- 管理环境(Manage environments)
- 管理全局变量(Manage globals)
- 开始运行一个集合
- 在集合运行器中使用环境
- 使用数据文件(Working with data files)
- 运行多次迭代(Running multiple iterations)
- 构建工作流程(Building workflows)
- 共享集合运行(Sharing a collection run)
- 调试集合运行(Debugging a collection run)
- 与 Newman 集成
- 与 Jenkins 集成
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
测试示例(Test examples)
测试示例
测试脚本在发送请求并从服务器收到响应后运行。
我们来看一些 Postman 测试的例子。大多数这些在 Postman 中可以作为片段。大多数测试与单行 JavaScript 语句一样简单。您可以根据要求提供尽可能多的测试。
设置环境变量
postman.setEnvironmentVariable("key", "value");
将嵌套对象设置为环境变量
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));
获取环境变量
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"));
清除环境变量
postman.clearEnvironmentVariable("key");
设置一个全局变量
postman.setGlobalVariable("key", "value");
获取全局变量
postman.getGlobalVariable("key");
清除一个全局变量
postman.clearGlobalVariable("key");
检查响应体是否包含一个字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
将 XML body 转换为 JSON 对象
var jsonObject = xml2Json(responseBody);
检查响应体是否等于一个字符串
tests["Body is correct"] = responseBody === "response_body_string";
检查 JSON 值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
内容类型存在(不区分大小写的检查)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
内容类型存在(区分大小写)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
响应时间小于 200ms
tests["Response time is less than 200ms"] = responseTime < 200;
响应时间在特定范围内(下限包含,上限排除)
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.1
状态码是 200
tests["Status code is 200"] = responseCode.code === 200;
状态码名称包含一个字符串
tests["Status code name has string"] = responseCode.name.has("Created");
成功的 POST 请求状态码
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
将 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);
解码 base64 编码数据
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = 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-js
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate); // a check for non-emptiness
示例数据文件
JSON 文件由键/值对组成。
对于 CSV 文件,顶行需要包含变量名。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论