Postman系列之Tests断言

Posted 软件测试小dao

tags:

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

以下,主要介绍如何用Postman的Tests这个模块进行断言,包括Tests断言原理介绍、常用断言方法以及Tests断言实例。

Tests断言介绍

1

简介

一个完整的接口测试,包括:请求>获取响应正文>断言。所谓断言,就是结果和预期对比,如果一致,则用例通过,如果不一致,断言失败,用例执行失败。

2

原理

Postman中的断言通过javascript语言编写,在Tests下方给出可以选择的一些断言内容,断言会在请求返回之后显示,并根据断言的PASS、Fail情况体现在最终测试结果中。
在这里插入图片描述
3

常用断言方法

Setting an environment variable :设置一个环境变量

pm.environment.set(“variable_key”, “variable_value”);
Setting a nested object as an environment variable : 将嵌套对象设置为环境变量

var array = [1, 2, 3, 4];
pm.environment.set(“array”, JSON.stringify(array, null, 2));

var obj = { a: [1, 2, 3, 4], b: { c: ‘val’ } };
pm.environment.set(“obj”, JSON.stringify(obj));
Getting an environment variable : 获取环境变量

pm.environment.get(“variable_key”);
Getting an environment variable (whose value is a stringified object) :获取一个环境变量,其值是一个字符串化的对象

// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.

var array = JSON.parse(pm.environment.get(“array”));
var obj = JSON.parse(pm.environment.get(“obj”));
Clear an environment variable : 清除一个环境变量

pm.environment.unset(“variable_key”);
Set a global variable :设置一个全局变量

pm.globals.set(“variable_key”, “variable_value”);
Get a global variable :获取一个全局变量

pm.globals.get(“variable_key”);
Clear a global variable : 清除全局变量

pm.globals.unset(“variable_key”);
Get a variable : 获取一个变量

pm.variables.get(“variable_key”);
Check if response body contains a string : 检查响应主体是否包含字符串

pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
});
Check if response body is equal to a string :检查响应主体是否等于一个字符串

pm.test(“Body is correct”, function () {
pm.response.to.have.body(“response_body_string”);
});
Check for a JSON value :检查JSON值

pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
Content-Type is present :内容类型存在

pm.test(“Content-Type is present”, function () {
pm.response.to.have.header(“Content-Type”);
});
Response time is less than 200ms :响应时间小于200ms

pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Status code is 200 :状态码是200

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});
Code name contains a string :代码名称包含一个字符串

pm.test(“Status code name has string”, function () {
pm.response.to.have.status(“Created”);
});
Successful POST request status code : 成功的POST请求状态码

pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
Send an asynchronous request :发送异步请求

pm.sendRequest(“https://postman-echo.com/get”, function (err, response) {
console.log(response.json());
});
Convert XML body to a JSON object :将XML正文转换为JSON对象

var jsonObject = xml2Json(responseBody);

Tests断言实例
1

环境准备
接口地址:https://www.v2ex.com/api/nodes/show.json?name=python

Method: GET
Authentication: None
2

场景

1.状态码等于200;

2.断言 id=90;

3.url = “http://www.v2ex.com/go/python”;

3

示例

1.在SNIPPETS中,找到"status code:Code is 200",此断言方法即判断HTTP状态码是否为200,点击此断言方法,断言代码自动添加至Tests下。
在这里插入图片描述
在这里插入图片描述
2.依次添加其他断言条件,点击Send发送请求。

断言条件:

pm.test(“Status code is 200”, function () {
//断言状态码是否为200
pm.response.to.have.status(200);
});

var jsonData = JSON.parse(responseBody);

//断言id是90
tests[“Check respose id value”] = jsonData.id === 90;

//断言url
tests[“Check respose url value”] = jsonData.url === “https://www.v2ex.com/go/python”;
如图所示:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

最后:【可能给予你一定的帮助】

在这里插入图片描述

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
关注我的微信公众号【软件测试小dao】免费获取~

我的学习交流群:644956177 群里有技术大牛一起交流分享~

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!

以上是关于Postman系列之Tests断言的主要内容,如果未能解决你的问题,请参考以下文章

第三篇 Postman之 Tests(后置处理器,断言)

3. Postman Tests断言(转)

利用 Postman 中 Tests 断言校验返回结果

利用 Postman 中 Tests 断言校验返回结果

postman添加断言

postman 使用 及断言