Postman中文文档——测试示例(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文件由键/值对组成。

下载JSON文件

对于CSV文件,顶行需要包含变量名。

下载CSV文件

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,186评论 19 139
  • Postman沙盒 Postman Sandbox是一个JavaScript执行环境,您可以在编写预请求脚本和测试...
    千夜阅读 12,418评论 0 10
  • 02 你的收获 你如果耐心的看完这篇文章,你会获得些许收获 对postman的使用有一个基(全)本(面)认知 可以...
    亭子青年阅读 29,744评论 0 18
  • 一、Pre Request Scripts Postman v0.10+ 版本支持pre-request scri...
    常大鹏阅读 68,597评论 4 69
  • 慌慌张张,匆匆忙忙的一年又过去了。掐指一算,我已经踏出校园一年整。去年的12月26日,我带着在我在学校积攒下...
    郭文伽阅读 105评论 0 0