本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/116660097
之前分享过 JMeter、Postman接口测试之读取csv文件、参数化。这次来说些断言的。
个人博客:https://blog.csdn.net/zyooooxie
需求:一组参数值,返回的某字段值不同,如何断言
某项目有个接口,是传入某int,返回值 类似:“data”:[{“month”:1,“csdn”:“zyooooxie”},{“month”:3,“csdn”:“zyooooxie”},{“month”:6,“csdn”:“zyooooxie”}……]
细说:
传50时,data list只有一个元素,校验month的字段值为1;
传500时,data list有两个元素,校验month的字段值为1, 3;
传1500时,data list有三个元素,校验month的字段值为1, 3, 6;
传2500时,data list有四个元素,校验month的字段值为1, 3, 6, 9;
传4000时,data list有五个元素,校验month的字段值为1, 3, 6, 9, 12;
这样的情形下,我想对返回的字段值做断言;要咋做呢?
可以按部就班,发5个请求,传某int,做 相对应的断言;
但我想把这个断言也做成参数化,怎么实现呢?
我的思路:准备一个CSV文件,将参数值、断言一起写入;使用工具(JMeter、Postman),读取每行的数据,将其设置为变量p、a(可能有a1、a2、a3、a4、a5);请求参数 使用p,得到响应后,将对应的a 和实际获得的返回值做断言。
参数化文件
有些断言值 故意设错
第1种 JMeter适用:
p,a1,a2,a3,a4,a5
50,1
500,1,3
1500,11,3,6
2500,1,31,6,9
4000,11,3,6,9,12
第2种 Postman 适用:
p,a1,a2,a3,a4,a5
50,1,NULL,NULL,NULL,NULL
500,1,3,NULL,NULL,NULL
1500,11,3,6,NULL,NULL
2500,1,31,6,9,NULL
4000,11,3,6,9,12
Mock返回值
我本地 建了 5个json文件,作为 请求时的返回值。
{"success":true,"errorCode":null,"errorMessage":null,"data":[{"month":1,"csdn":"zyooooxie"}]}
{"success":true,"errorCode":null,"errorMessage":null,"data":[{"month":1,"csdn":"zyooooxie"},{"month":3,"csdn":"zyooooxie"}]}
{"success":true,"errorCode":null,"errorMessage":null,"data":[{"month":1,"csdn":"zyooooxie"},{"month":3,"csdn":"zyooooxie"},{"month":6,"csdn":"zyooooxie"}]}
{"success":true,"errorCode":null,"errorMessage":null,"data":[{"month":1,"csdn":"zyooooxie"},{"month":3,"csdn":"zyooooxie"},{"month":6,"csdn":"zyooooxie"},{"month":9,"csdn":"zyooooxie"}]}
{"success":true,"errorCode":null,"errorMessage":null,"data":[{"month":1,"csdn":"zyooooxie"},{"month":3,"csdn":"zyooooxie"},{"month":6,"csdn":"zyooooxie"},{"month":9,"csdn":"zyooooxie"},{"month":12,"csdn":"zyooooxie"}]}
Moco框架 返回本地文件
常常用的是 直接写死返回值,但这次想着搞点不同的:response中 返回本地的文件。
下面提供2种方式:
moco 直接返回本地文件
https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md#attachment
File: all.json
[
{"description":"send request 50",
"request": {"uri": "/zyooooxie","method":"get","queries":{"zy":"50","test":"TEST"}},
"response":{"file":"50.json","headers" :{"content-type" : "application/json"}}},
{"description":"send request 500",
"request": {"uri": "/zyooooxie","method":"get","queries":{"zy":"500","test":"TEST"}},
"response":{"file":"500.json","headers" :{"content-type" : "application/json"}}},
{"description":"send request 1500",
"request": {"uri": "/zyooooxie","method":"get","queries":{"zy":"1500","test":"TEST"}},
"response":{"file":"1500.json","headers" :{"content-type" : "application/json"}}},
{"description":"send request 2500",
"request": {"uri": "/zyooooxie","method":"get","queries":{"zy":"2500","test":"TEST"}},
"response":{"file":"2500.json","headers" :{"content-type" : "application/json"}}},
{"description":"send request 4000",
"request": {"uri": "/zyooooxie","method":"get","queries":{"zy":"4000","test":"TEST"}},
"response":{"file":"4000.json","headers" :{"content-type" : "application/json"}}}
]
moco的template用法 返回本地文件
https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md#file-name-template
File: new.json
[
{
"description":"send request",
"request": {"uri": "/zyooooxie","method":"get","queries":{"test":"TEST"}},
"response":{ "file": {"name": {"template": "${req.queries['zy']}.json"}},"headers" :{"content-type" : "application/json"}}
}
]
使用requests请求
def send_request(req_num: int):
data_dict = {"zy": req_num, "test": "TEST"}
res = requests.get('http://127.0.0.1:12306/zyooooxie', params=data_dict)
print(res.json())
data_list = res.json()['data']
order_list = [d['month'] for d in data_list]
return order_list
JMeter来做
我的思路:
1,CSV Data Set Config,设置1个传参变量、5个断言变量【某些断言变量没赋值,就不存在】;
2,If Controller 判断某断言变量是否存在【不同判断条件 对应 不同断言内容】;
https://jmeter.apache.org/usermanual/component_reference.html#If_Controller
3,使用传参变量发请求;
4,Regular Expression Extractor 后置提取 实际返回值中 所有断言的value;
5,断言(N个断言变量的值 和 实际返回值);
6,结果
Postman来做
我的思路:
0,请求中 传参为 本地CSV文件中的变量;断言变量需要 特别留意【每个断言变量都要有值,我设置为NULL(读取后是str格式)】;
1,请求中,请求体 使用传参变量;
2,后置Tests脚本:if判断 某断言变量是否为’NULL’,若不为’NULL’,断言此断言变量的值 和实际返回值;
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].month).to.eql(pm.variables.get("a1"));
if (pm.variables.get("a2") != "NULL"){
pm.expect(jsonData.data[1].month).to.eql(pm.variables.get("a2"));
}
if (pm.variables.get("a3") != "NULL"){
pm.expect(jsonData.data[2].month).to.eql(pm.variables.get("a3"));
}
if (pm.variables.get("a4") != "NULL"){
pm.expect(jsonData.data[3].month).to.eql(pm.variables.get("a4"));
}
if (pm.variables.get("a5") != "NULL"){
pm.expect(jsonData.data[4].month).to.eql(pm.variables.get("a5"));
}
}
);
3,Collection中 Data设置使用本地CSV文件、Iteration设置请求次数、Preview Data查看实际读取的数据;
本文链接:https://blog.csdn.net/zyooooxie/article/details/116660097
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie