postman测试实例--断言

让我们来看看postman测试的一些例子。 其中大部分是作为内部postman片段。 大多数测试是为单行的JavaScript语句一样简单。 只要你想一个请求,你可以有很多的测试。

注意:一个响应已从服务器接收后测试脚本运行。



测试实例


1.设置环境变量
 postman.setEnvironmentVariable("key", "value");
 例子: postman.setEnvironmentVariable("url", "http://192.168.36.47/v2/api");
使用环境变量的格式:{{url}}

1.1清除环境变量
postman.clearEnvironmentVariable("variable_key");
例子:postman.clearEnvironmentVariable("url");


2.设置一个全局变量
postman.setGlobalVariable("key", "value");
例子:postman.setGlobalVariable("username", "123@qq.com");
使用全局变量格式:{{variableName}}

2.1清除一个全局变量
postman.clearGlobalVariable("key", "value");
例子:postman.clearGlobalVariable("username", "123@qq.com");

3.检查响应体包含一个字符串
 tests["Body matches string"] = responseBody.has("string_you_want_to_search");
 例子:响应体包含以下字段 "path": "field is read-only",
tests["Body matches string"] = responseBody.has("field is read-only");
tests["Body matches string"] = responseBody.has("path");

4.转换XML身体JSON对象
 var jsonObject = xml2Json(responseBody);
例子:

5.检查响应体等于一个字符串
 tests["Body is correct"] = responseBody === "response_body_string";
例子:响应体包含以下字段 "path": "field is read-only",
tests["Body is correct"] = responseBody === "response_body_string";

6.检查一个JSON值

 var data = JSON.parse(responseBody);
 tests["Your test name"] = data.value === 100;

7.Content-Type的存在(不区分大小写检查)
 
 tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

8.Content-Type的存在(区分大小写)
 
 tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.响应时间小于200ms的

 tests["Response time is less than 200ms"] = responseTime < 200;

10.状态代码是200

 tests["Status code is 200"] = responseCode.code === 200;
 例子:状态码是404
 tests["Status code is 404"] = responseCode.code === 404;

11.代号包含一个字符串

 tests["Status code name has string"] = responseCode.name.has("Created");
例子:Status:201 CREATED
 tests["Status code is 201"] = responseCode.code === 201;
 tests["Status code name has string"] = responseCode.name.has("Created");

12.成功的POST请求的状态代码

 tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

13.使用TinyValidator的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);

样本数据文件

JSON文件由键/值对
下载JSON文件

对于CSV文件,最上面一行需要包含变量名
下载CSV文件

转载于:https://www.cnblogs.com/yy-cxd/p/5818827.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值