当前位置:网站首页>接口自动化测试框架postman tests常用方法
接口自动化测试框架postman tests常用方法
2022-08-05 00:52:00 【软测小西瓜】
目录
3.检查response body中是否包含某个string
6.检查response body是否与某个string相等
7.测试response Headers中的某个元素是否存在(如:Content-Type)
14.JSON.parse()和JSON.stringify()
postman常用方法集合:
1.设置环境变量
postman.setEnvironmentVariable("key", "value");
pm.environment.set("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);13.获取request.data值:
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.
假设requestBody中有"version":"1.0";这个值,如果想获取到version的value值,代码如下
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" )边栏推荐
- CNI(Container Network Plugin)
- 僵尸进程和孤儿进程
- VOC格式数据集转COCO格式数据集
- 2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
- Binary tree [full solution] (C language)
- 5. PCIe official example
- torch.autograd.grad finds the second derivative
- Helm Chart
- ORA-00604 ORA-02429
- GCC: Shield dependencies between dynamic libraries
猜你喜欢

LiveVideoStackCon 2022 上海站明日开幕!

快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名

sqlite--nested exception is org.apache.ibatis.exceptions.PersistenceException:

进程间通信和线程间通信

gorm joint table query - actual combat

JUC thread pool (1): FutureTask use

活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!

Getting Started with Kubernetes Networking

深度学习原理学习小结 - Self-Attention/Transformer
![[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy](/img/33/3177b4c3de34d4920d741fed7526ee.png)
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy
随机推荐
Matlab uses plotting method for data simulation and simulation
手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
2021年11月网络规划设计师上午题知识点(下)
2022 Nioke Multi-School Training Session H Question H Take the Elevator
【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍
After the staged testing is complete, have you performed defect analysis?
2022 Multi-school Second Session K Question Link with Bracket Sequence I
JWT简单介绍
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 1)
Getting Started with Kubernetes Networking
matlab 采用描点法进行数据模拟和仿真
The principle of NMS and its code realization
GCC: Shield dependencies between dynamic libraries
【翻译】CNCF对OpenTracing项目的存档
Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
Memory Forensics Series 1
ora-00604 ora-02429
linux(centOs7)部署mysql(8.0.20)数据库