postman tests常用方法
postman常用方法集合:
1.设置环境变量
postman.setEnvironmentVariable("key", "value");
pm.environment.get("key", "value");//postman 5.0以上版本设置环境变量的方法
2.设置全局变量
postman.setGlobalVariable("key", "value");
pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本设置全局变量方法
3.检查response body中是否包含某个string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});//5.0以上版本方法
4.检测JSON中的某个值是否等于预期的值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。
如:检查json中某个数组元素的个数(这里检测programs的长度)
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;
5.转换XML body为JSON对象
var jsonObject = xml2Json(responseBody); tests["Body is correct"] = responseBody === "response_body_string";
6.检查response body是否与某个string相等
7.测试response Headers中的某个元素是否存在(如:Content-Type)
//getResponseHeader()方法会返回header的值,如果该值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
上面的方法,不区分大小写。下面的方法,要区分大小写。

8.验证Status code的值
tests["Status code is 200"] = responseCode.code === 200;
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});//5.0以上版本方法
9.验证Response time是否小于某个值
tests["Response time is less than 200ms"] = responseTime < 200; //5.0以上版本方法
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
10.name是否包含某个值
tests["Status code name has string"] = responseCode.name.has("Created");
//5.0以上版本方法
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
11.POST 请求的状态响应码是否是某个值
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202; //5.0以上版本方法
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
12.很小的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);


var Json = JSON.parse(request.data);
data {object}:
this is a dictionary of form data for the request. (request.data["key"]=="value")headers {object}:
this is a dictionary of headers for the request (request.headers["key"]=="value")method {string}:
GET/POST/PUT etc.url {string}:
the url for the request.
var Json = JSON.parse(request.data);
var version = Json["version"];
14.JSON.parse()和JSON.stringify()


JSON.parse()【从一个字符串中解析出json对象】
JSON.stringify()【从一个对象中解析出字符串】 var data={name:'goatling'}
JSON.parse(data)
结果是: '{"name":"goatling"}' JSON.stringify(data)
结果是:name:"goatling"


15.判断字段值是否为空typeof()
var Jsondata = JSON.parse(responseBody);
if( typeof(Jsondata.data) != "undefined" )
postman tests常用方法的更多相关文章
- postman tests实例记录(还没看,一些常用的)
这段时间准备测试api接口,postman这个工具很是方便,特别是里面的tests的javascript脚本. 记录一下测试接口常用的tests验证的实例. 1.设置环境变量 postman.setE ...
- postman Tests断言
摘要:关于postman的断言方法很多,在网上随便搜寻下,能搜出一大推,什么牛鬼蛇神都有,让人眼花缭乱..甚至在应用时出现错误.Test断言都是根据js规则来写的,对于我这种不懂js语言的来说确实不友 ...
- Postman Tests脚本的使用
直接在Tests中写js代码断言结果,Test Result展示运行结果,简单方便. 示例脚本: var jsonData = JSON.parse(responseBody); var num = ...
- 接口测试postman深度挖掘应用③--postman终结篇
上一章节我们介绍了postman的变量测试以及导入数据测试基本上技术性的东西已经差不过了,这篇文章再系统性的介绍一下. 一.下载 官网:https://www.postman.com 1.选择需要下载 ...
- postman简单教程,如何在请求中引用上次请求返回的值
做接口测试,一定会遇到这种情况,需要拿上次请求的值在本次请求中使用,比如,我们去测试一个东西,要去登录才能做其他的操作,需要拿到登录返回数据中的某些字段,比如,token啊等... 如果发一次请求,就 ...
- postman 官方 test 脚本样例
Test examples 样例来源: https://learning.getpostman.com/docs/postman/scripts/test_examples/ Test scripts ...
- postman具体讲解
postman 简单教程-实现简单的接口测试 最近开始做接口测试了,因为公司电脑刚好有postman,于是就用postman来做接口测试,哈哈哈哈,...postman 功能蛮强大的,还比较好用,下面 ...
- postman的关联,即如何在请求中引用上次请求返回的值
做接口测试,一定会遇到这种情况,需要拿上次请求的值在本次请求中使用,比如,我们去测试一个东西,要去登录才能做其他的操作,需要拿到登录返回数据中的某些字段,比如,token啊等... 如果发一次请求,就 ...
- 4、postman的常见断言
推荐我的另一篇文章 浅谈JSONObject解析JSON数据,这篇文章原理类似,使用java或者beanshell进行断言解析json数据 介绍断言之前,我们先测试1个接口: 接口地址:https: ...
随机推荐
- echarts动态刷新数据
在这次的项目中图表显示的部分比较多,这边给分享下用到的图表的数据刷新 饼图最后的效果 先看下 前端部分 <div div style="height: 40%; width: 17.5 ...
- ISCONF Redis is configured to save RDB snapshots
MISCONF Redis is configured to save RDB snapshots redis报错: (error) MISCONF Redis is configured to sa ...
- centos7上以RPM方式安装MySQL5.6
1. 下载MySQL http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/ MySQL-5.6.36-1.el7.src.rpm MySQL-5.6.36- ...
- Python2.x安装教程及环境变量配置
下载Python Python的官网是:http://www.python.org/ 进入官网,也可以找到对应的下载页面:http://www.python.org/download/ 安 ...
- python面向对象<三>
类属性.实例属性: class Tool(object): #属性(类属性)类对象(Tool) num = 0 #方法 def __init__(self,new_name): self.name = ...
- mysql数据库基本操作语句
1 更改字段名:change alter table student change column gradenews grade int(11); 2 增加字段和删除字段 alter table s ...
- U盘安装centos7 系统卡在 starting dracut initqueue hook
U盘安装centos7启动过程中出现: [ok] Reached target Basic System 或者 [ok] starting dracut initqueue hook 下面是我解决的过 ...
- 一个类GraphQL的ORM数据访问框架发布
Zongsoft.Data 发布公告 很高兴我们的 ORM 数据访问框架(Zongsoft.Data)在历经两个 SaaS 产品的应用之后,今天正式宣布对外推广! 这是一个类 GraphQL 风格的 ...
- Batch批处理获取当前时间
这不是一个新问题,但是由于网上写的都是针对自己的电脑设置,没有通用性,而我呢,又需要在不同电脑上使用,因此,这命题一个问题了.其实也没有什么好说的,直接上代码. @ECHO OFF set split ...
- pat 1054 The Dominant Color(20 分)
1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...