Postman断言

Postman断言

在做接口测试时,在某一些场景下需要添加断言,对接口进行判断。postman在“Tests”中提供了比较多的断言方式来帮助接口测试的断言。

说明:红色字体为postman内SNIPPETS下的样式,橙色Postman内test方法的原样例

1、 Get an environment variable
-> pm.environment.get(“variable_key”);
解释:获取环境变量
实例:
var cs1 = pm.environment.get(“account”);
console.log(cs1)
在这里插入图片描述
2、 Get a global variable
-> pm.globals.get(“variable_key”);
解释:获取全局变量
实例:
var cs2 = pm.globals.get(“city_1”);
console.log(cs2)
在这里插入图片描述
3、 Get a variable
-> pm.variables.get(“variable_key”);
解释:获取一个变量
实例:
var cs3 = pm.variables.get(“city_2”); //
console.log(cs3)
在这里插入图片描述
4、 Set an environment variable
-> pm.environment.set(“variable_key”, “variable_value”);
解释:设置环境变量,variable_key为变量名,variable_value变量值
实例:
var cs4 = pm.environment.set(“cs_value1”, “123”);
在这里插入图片描述
5、 Set a global variable
-> pm.globals.set(“variable_key”, “variable_value”);
解释:设置全局变量,variable_key为变量名,variable_value变量值
实例:
var cs5 = pm.globals.set(“cs_value2”, “456”);
在这里插入图片描述
6、 clear an environment variable
-> pm.environment.unset(“variable_key”);
解释:清除一个环境变量
实例:
var cs6 = pm.environment.unset(“cs_value1”);
在这里插入图片描述
7、 clear a global variable
-> pm.globals.unset(“variable_key”);
解释:清除一个全局变量
实例:
var cs7 = pm.globals.unset(“cs_value2”);
在这里插入图片描述
8、 send a requent
-> pm.sendRequest(“https://postman-echo.com/get”, function (err, response) {
console.log(response.json());
});

解释:其中,https://postman-echo.com/get表示要发送的请求
function中的err表示请求返回的错误信息,response表示响应内容
console.log()是postman封装的查看日志的方法,可以调出postman的console控制台来查看代码运行情况,方便调试。
想要打印出什么由自己来定义,上面表示打印出json格式的响应信息

9、’ Status code:code is 200
-> pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

解释:判断响应码是否为200
实例:
pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});
在这里插入图片描述
10、 Response body:contains string
-> pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
});

解释:判断返回的body中包含的字符串,Body matches string可以自己命名,string_you_want_to_search表示要找的body里面的字符信息
实例:
pm.test(“判断包含success”, function () {
pm.expect(pm.response.text()).to.include(“success”);
});
在这里插入图片描述
11、 Response body:json value check
-> pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});

解释:检测返回字段值与预期是否相等,有三种写法

  • 法一:postman方法将字符串转换成json对象
    pm.test(“测试返回的result的值”, function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.result).to.eql(“success”);
    });
    在这里插入图片描述
  • 法二:JSON语法将字符串转换成json对象
    pm.test(“测试返回的dataMap的值”, function () {
    var jsonData =JSON.parse(responseBody);
    jsonData.dataMap===true;
    });
  • 法三:使用tests方法来进行断言
    var jsondata =JSON.parse(responseBody);
    tests[“测试返回的code=200”]=jsondata.code===“200”;

12、response body:is equal to a string
-> pm.test(“Body is correct”, function () {
pm.response.to.have.body(“response_body_string”);
});

解释:检查响应主体是否等于一个字符串,Body is correct == response_body_string
实例:
pm.test(“Body is correct”, function () { pm.response.to.have.body(’{“result”:“success”,“locate”:"\/sys\/index.php?m=index&f=index"}’);
});
在这里插入图片描述
13、Response headers : content-Tye header check
-> pm.test(“Content-Type is present”, function () {
pm.response.to.have.header(“Content-Type”);
});

解释:内容类型存在,tests[“Content-Type is present”] = postman.getResponseHeader(“ContentType”); 参数:预期header
实例:
pm.test(“Content-Type is present”, function () {
pm.response.to.have.header(“Date”);
});
在这里插入图片描述
14、Response time is less than 200ms
-> pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

解释:响应时间小于200ms
实例:
pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
在这里插入图片描述
15、Status code:successful post request
-> pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

解释:成功的POST请求状态码
实例:
pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([200,201]);
});
在这里插入图片描述
16、Status code : code name has string
-> pm.test(“Status code name has string”, function () {pm.response.to.have.status(“Created”); });
解释:代码名称包含一个字符串
实例:
pm.test(“Status code name has string”, function () {
pm.response.to.have.status(“OK”);
});
在这里插入图片描述
17、Response body: convert xml body to a json object
-> var jsonObject = xml2Json(responseBody);
解释:将XML正文转换为JSON对象

18、Use Tiny Validator for JSON data
-> var schema = {
“items”: {
“type”: “boolean”
}
}
var data1 = [true, false];
var data2 = [true, 123];
pm.test(‘Schema is valid’, function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});

解释:为JSON数据使用TinyValidator

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值