postman常用断言

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postman常用断言相关的知识,希望对你有一定的参考价值。

参考技术A 1.获取响应数据,并转化为json格式
var resdata = pm.response.json();
console.info(resdata);

2.获取环境变量的值
var key = pm.environment.get("variable_key");
例如:
var url = pm.environment.get("url")
console.info(url);

3.获取全局变量的值
var key = pm.globals.get("variable_key");
例如:
var channelName = pm.globals.get("channelName");
console.info(channelName);

4.响应时间小于200
pm.test("Response time is less than 200ms", function ()
pm.expect(pm.response.responseTime).to.be.below(200);
);

5.校验返回结果中,某个参数的值,是否等于某值
pm.test("返回结果是否ret:0", function ()
var jsonData = pm.response.json(); //将整个返回结果,赋值给变量jsonData
pm.expect(jsonData.ret).to.eql("0"); //赋值返回结果的变量jsonData下的参数package下的ret,是否=0
);

6.清除一个全局变量
pm.globals.unset(“variable_key”);

7.清除一个环境变量
pm.environment.unset(“variable_key”);

8.检查响应主体是否包含字

pm.test(“Body matches string”, function ()
pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
);

9.Content-Type 存在
pm.test(“Content-Type is present”, function ()
pm.response.to.have.header(“Content-Type”);
);

postman断言分析

最近测试中用到postman,使用后就简单总结下常用的断言,下面带图的自己最常用的,其他的没怎么用。

postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。

断言会在请求返回之后,运行,并根据断言的pass\\fail情况体现在最终测试结果中。

 

1.设置环境变量--Setting an environment variable 

postman.setEnvironmentVariable("key", "value");

2.设置全局变量--Set a global variable

postman.setGlobalVariable("key", "value");

3.检查响应中**string--Check if response body contains a string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

4.转化XML格式的响应成JSON对象---Convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

5.检查响应body中等于指定string--Check if response body is equal to a string

 tests["Body is correct"] = responseBody === "response_body_string";

6.检查JSON某字段值--Check for a JSON value

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;

7.检查Content-Type是否**在header返回(大小写不敏感)--Content-Type is present (Case-insensitive checking)

 tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

8.检查Content-Type是否**在header返回(大小写敏感)--Content-Type is present (Case-sensitive)

 tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.检查请求耗时时间小于200ms--Response time is less than 200ms

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

10.检查Status code为200--Status code is 200

tests["Status code is 200"] = responseCode.code === 200;

11.检查Code name**指定string--Code name contains a string

 tests["Status code name has string"] = responseCode.name.has("Created");

12.检查成功post的请求status code--Succesful POST request status code

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

13.为JSON data使用微小验证器--Use TinyValidator for JSON data

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);

Sample data files 

JSON files are composed of key/value pairs

 

以上是关于postman常用断言的主要内容,如果未能解决你的问题,请参考以下文章

postman断言内容详解

postman断言之常用函数

postman响应断言

Postman系列之Tests断言

postman 断言解析

postman断言作用及怎么使用