如何对返回结果的列表设置检查点

背景:

接口检查点是常用的一种接口测试方法,一般都会设置断言来进行简单的检查。但有关返回结果是检查好几个字段的字符串,这一般不常见。

实例

类似下图所示:
请添加图片描述
给角色分配菜单权限,对应的请求信息:
请添加图片描述

为确保分配成功需要查询并做相应的检查项,如果上面接口有返回分配的信息那就不用再调用查询接口,如果没有那就需要查询进行核对。我这个例子里分配接口里的是返回值只有成功与失败标识并没有成功与失败的详细信息。所以需要调用对应的查询接口进行检测。如下图:
请添加图片描述

返回是一个标准的json格式,为便于分析利用json在线工具,对其进行美化一下
请添加图片描述
可以看到父节点id与子节点id。

目的

就是让设置的菜单id与实际取到的id相比较,如果一致则表示设置成功;否则,设置失败。

解析

首先我们通过过虑提取出需要的id.

因返回值是一个标准json格式:

{"code":200,"message":"查询成功","data":[{"id":1,"parentId":null,"code":"appAuth","name":"应用授权","url":null,"description":null,"sort":1,"children":[{"id":10,"parentId":1,"code":"appDetail","name":"应用概览","url":null,"description":null,"sort":0,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":11,"parentId":1,"code":"appInspect","name":"应用体检","url":null,"description":null,"sort":1,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":12,"parentId":1,"code":"sysJurisdictionView","name":"应用视图","url":null,"description":null,"sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":13,"parentId":1,"code":"__ACCOUNT__","name":"账号授权","url":null,"description":null,"sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":23,"parentId":1,"code":"DEPT_ROLE","name":"部门角色","url":null,"description":null,"sort":3,"children":[],"createAt":"2021-07-02 18:52:11","selMenuStatus":true},{"id":14,"parentId":1,"code":"__ORGANIZATION__","name":"机构授权","url":null,"description":null,"sort":4,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":15,"parentId":1,"code":"__POST__","name":"岗位授权","url":null,"description":null,"sort":5,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":16,"parentId":1,"code":"__GROUP__","name":"群组授权","url":null,"description":null,"sort":6,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":17,"parentId":1,"code":"groupClassify","name":"群组分类","url":null,"description":null,"sort":7,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":18,"parentId":1,"code":"ROLE","name":"角色授权","url":null,"description":null,"sort":8,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":19,"parentId":1,"code":"ROLE_CLASSIFY","name":"角色分类","url":null,"description":null,"sort":9,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":20,"parentId":1,"code":"ROLE_GROUP","name":"角色组授权","url":null,"description":null,"sort":10,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":21,"parentId":1,"code":"auth","name":"功能权限","url":null,"description":null,"sort":11,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":22,"parentId":1,"code":"ROLE_ALLOT","name":"分权管理设置","url":null,"description":null,"sort":12,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true}],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":2,"parentId":null,"code":"userAuth","name":"用户授权","url":null,"description":null,"sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":3,"parentId":null,"code":"appManager","name":"应用管理","url":null,"description":null,"sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":4,"parentId":null,"code":"module","name":"模型定义","url":null,"description":null,"sort":4,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":5,"parentId":null,"code":"ruleEngine","name":"规则引擎","url":null,"description":null,"sort":5,"children":[{"id":51,"parentId":5,"code":"bpglobalRole","name":"全局角色","url":null,"description":"","sort":1,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":52,"parentId":5,"code":"bppostRole","name":"岗位规则","url":null,"description":"","sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":53,"parentId":5,"code":"jurisdictionView","name":"岗位视图","url":null,"description":"","sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":true}],"createAt":"2021-07-02 09:57:42","selMenuStatus":true},{"id":6,"parentId":null,"code":"compliance","name":"合规管理","url":null,"description":null,"sort":6,"children":[{"id":61,"parentId":6,"code":"positionRule","name":"合规规则","url":null,"description":"","sort":1,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":62,"parentId":6,"code":"bpcMutex","name":"互斥规则","url":null,"description":"","sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":63,"parentId":6,"code":"bpcRuleScan","name":"规则扫描","url":null,"description":"","sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":64,"parentId":6,"code":"positionRuleDetail","name":"用户合规审计","url":null,"description":"","sort":4,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":65,"parentId":6,"code":"bpcMutexDetail","name":"应用合规审计","url":null,"description":"","sort":5,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":66,"parentId":6,"code":"sensitivePermissions","name":"敏感权限","url":null,"description":null,"sort":6,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false}],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":7,"parentId":null,"code":"statistics","name":"统计分析","url":null,"description":null,"sort":7,"children":[{"id":71,"parentId":7,"code":"accountStatistics","name":"账号状态分析","url":null,"description":"","sort":1,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":72,"parentId":7,"code":"userEffectiveRole","name":"账号角色数统计","url":null,"description":null,"sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":73,"parentId":7,"code":"moduleStatistics","name":"菜单授权数统计","url":null,"description":"","sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":74,"parentId":7,"code":"accessMode","name":"权限开通方式分析","url":null,"description":null,"sort":4,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":75,"parentId":7,"code":"applyStatistics","name":"自助申请趋势分析","url":null,"description":"","sort":5,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":76,"parentId":7,"code":"postAutomation","name":"自动开通权限分析","url":null,"description":null,"sort":6,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":77,"parentId":7,"code":"roleSimilarity","name":"角色相似度分析","url":null,"description":"","sort":7,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false}],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":8,"parentId":null,"code":"system","name":"系统管理","url":null,"description":null,"sort":8,"children":[{"id":81,"parentId":8,"code":"bpcAccount","name":"应用账号","url":null,"description":null,"sort":1,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":82,"parentId":8,"code":"bpcRole","name":"应用角色","url":null,"description":null,"sort":2,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":83,"parentId":8,"code":"bpcLog","name":"应用日志","url":null,"description":null,"sort":3,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":84,"parentId":8,"code":"bpcSkin","name":"应用换肤","url":null,"description":null,"sort":4,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false},{"id":85,"parentId":8,"code":"license","name":"license管理","url":null,"description":null,"sort":5,"children":[],"createAt":"2021-07-02 09:57:42","selMenuStatus":false}],"createAt":"2021-07-02 09:57:42","selMenuStatus":false}]}

我们可以通过JSON Extractor提取对应的id,提取表达式

$.data[?(@.selMenuStatus==true)].id;$.data[?(@.selMenuStatus==true)].children[?(@.selMenuStatus==true)].id
请添加图片描述

请添加图片描述

其次就是设置断言

我这里用到的是JRS223断言。
请添加图片描述


log.info("----------------------------------------------------------------");
//log.info(vars.get("childrenId_ALL"));
//获取提取的菜单子节点id,给childrenId
String childrenId=vars.get("childrenId_ALL");
//获取提取的菜单父节点id,给parentId
String parentId=vars.get("parentId_ALL");
//合并获取的节点id,给getMenuId
String getMenuId=childrenId+","+parentId+","+"66"
//获取设置的菜单id,menuId,也就是前面的【1,10,11,12,13,23,14,15,16,17,18,19,20,21,22,2,3,4,5,52,53】
String menuId = vars.get("menuId");
log.info(vars.get("menuId"));
//把字符串通过"逗号"分隔,拆分成字符串数组,给id
String[] id =getMenuId.split(",");
int n;
//for循环获取到的每一个id,并判断是否在设置的菜单menuId中
for(String s: id){
	//如果获取的id在设置的菜单menuId中,则表示设置功能菜单成功,不做任何处理继续执行for循环;
	if(menuId.contains(s)){
		continue;
//		log.info(s+"角色功能权限添加成功!"+menuId);
		}
	//如果获取的id不在设置的菜单menuId中,则表示设置功能菜单失败,设置断言失败,直接路上执行for循环;
	else{
		log.info(s+"角色功能权限添加失败");
//		boolean f = true;
		AssertionResult.setFailure(true);
		AssertionResult.setResultForFailure("角色添加的功能权限与查询到的功能权限不一致!");
//		prev.setResponseCode("300");
		break;
		}
	}
log.info("-----------------------------------------------------------------");

注:
本人是java初学者,只会一些简单的语法,所以在写这个脚本时用到的知识点也是各种的求助与度娘,好在最终还是给弄出来了。在此也进行总结汇总一下:

1、java特点:所有变量必须先声明再使用,否则会编译失败。
这一点跟python是不一样的,python可以不用声明,直接使用,并且会根据语境自动的断送给的变量值所属的类型。

2、java中字符串“String”转换字符串数组“String[]”。

3、判断子字符串“s”是否在给定字符串“menuId”中所用到的方法"parentString.contains(childrenString)"

4、动态设置断言的状态“AssertionResult.setFailure(boolean f)”,及提示信息“AssertionResult.setResultForFailure(String msg)”

最后说一点这次的心得 “Jmeter官方文件真的很好用!!!”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值