http://blog.csdn.net/lluozh2015/article/details/72834014
API接口测试中,对于接口请求respone的校验是非常重要的一个环节
在使用Jmeter进行接口测试时,有多种respone校验方式,比如响应断言、BeanShell断言等等,BeanShell断言可以自定义断言,自由灵活的用脚本实现断言
什么是BeanShell
小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法
运行其内部的脚本处理Java应用程序,还可以在运行过程中动态执行你java应用程序执行java代码,因为BeanShell是用java写的,运行在同一个虚拟机的应用程序,因此可以自由地引用对象脚本并返回结果
先看看这样的respone
- {
- "message": "不能发送小于当前时间点的定时任务",
- "statusCode": 200
- }
- 1
- 2
- 3
- 4
现在需要验证statusCode的值是否为200
- import org.json.*;
- import java.util.Arrays;
-
- //获取上一个请求的返回
- String jsonString = prev.getResponseDataAsString();
- JSONObject responseJson = new JSONObject(jsonString);
-
- //判断返回值是否和预期一致
- if (responseJson.getInt("statusCode") != 200) {
- //把断言失败置为真,即用例失败,并在结果树中显示FailureMessage
- Failure = true;
- FailureMessage = "statusCode的返回值有误";
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
如果要验证respone中message的值是否与预期一致,需要怎么做呢?
- import org.json.*;
- import java.util.Arrays;
-
- //获取上一个请求的返回
- String jsonString = prev.getResponseDataAsString();
- JSONObject responseJson = new JSONObject(jsonString);
- String fbpcontent = responseJson.getString("message");
- if (!fbpcontent.equals("不能发送小于当前时间点的定时任务")) {
- //把断言失败置为真,即用例失败,并在结果树中显示FailureMessage
- Failure = true;
- FailureMessage = "message与实际值不一致";
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
现有这样格式的respone数据
- {
- "statusCode": 200,
- "data": [
- {
- "i": "50356",
- "n": "项目一",
- "v": "2.0",
- "iconUrl": "",
- },
- {
- "i": "45280",
- "n": "项目二",
- "v": "3.0",
- "iconUrl": "",
- },
- {
- "i": "14656",
- "n": "项目三",
- "v": "2.6",
- "iconUrl": "",
- },
- {
- "i": "66213",
- "n": "项目四",
- "v": "5.0",
- "iconUrl": "",
- }
- ]
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
需要解析数组data的值,如何去解析呢?
- import org.json.*;
- import java.util.Arrays;
-
- //获取上一个请求的返回
- String jsonContent = prev.getResponseDataAsString();
-
- JSONObject response = new JSONObject(jsonContent);
- JSONArray groups = response.getJSONArray("data");
- String strData= groups.toString();
- log.info(strData)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
现在有更加复杂格式的respone数据
- {
- "priorityGroups": {
- "proId": 1234,
- "name": "项目一",
- "groups": [
- {
- "id": "50356",
- "items": [
- {
- "proId": 1360,
- "n": "PC端",
- "index": 1
- },
- {
- "proId": 1361,
- "n": "iOS端",
- "index": 2
- },
- {
- "proId": 1362,
- "n": "安卓端",
- "index": 4
- }
- ]
- }
- ]
- },
- "promotion": {
- "proId": 1364,
- "cusId": 84,
- "name": "项目二",
- "from": 1470821215,
- "to": 1470907615,
- "status": 1,
- "objectId": 1069,
- "createBy": 394,
- "eff": 1470821215000,
- "createTime": 1470821155000
- }
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
如果需要解析groups中的数据,需要怎么实现呢?
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
-
- String jsonContent = prev.getResponseDataAsString();
-
- JSONObject response = new JSONObject(jsonContent);
- JSONArray groups = response.getJSONObject("priorityGroups").getJSONArray("groups");
- String strGroups = groups.toString();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
BeanShell的获取数据解析,还可以使用Gson提供的方法
- //prev.getResponseDataAsString是Jmeter提供的方法,可以调取上次请求的响应字符串
- response = prev.getResponseDataAsString();
-
- //使用Gson提供的方法解析json
-
- JsonParser parser = new JsonParser();
- JsonObject responseObj = (JsonObject) parser.parse(response);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
转载于:https://www.cnblogs.com/jtestroad/p/8459439.html
浙公网安备 33010602011771号