问题来源:
接口a返回一个结果aa,抽取出变量aaa,接口b返回一个结果bb,抽取出变量bbb,想要通过对比aaa和bbb来判断接口返回的正确与否。因为不是直接判断接口的返回,不能用Response Assertion,所以使用BeanShell Assertion。
BeanShell Assertion是什么?
它允许用户编写java脚本来进行断言。
实现方法:
根据图中的步骤新建BeanShell Assertion

添加java代码:
String new_message_count_from_msgcount=vars.get("new_message_count_from_msgcount");
String new_message_count_from_msglist=vars.get("new_message_count_from_msglist_matchNr");
if (!new_message_count_from_msgcount.equals(new_message_count_from_msglist)){
Failure=true;
FailureMessage="从mine_message_list中获取的新消息数不等于从mine_message_count中获取的新消息数";
}
如何写java脚本?
在jmeter的里面的变量都可以通过vars.get("new_message_count_from_msglist_matchNr");这种方式取出变量。取出变量new_message_count_from_msglist_matchNr(正则提取器提取的数据)赋值给一个java的变量,以在java代码中使用该变量。

上图中红框2里面内容:
Read/Write: Failure, FailureMessage, SampleResult, vars, props, log.
这几个变量都是可读写的,Failure是assertion的结果,如果该值为true则断言不通过;FailureMessage是断言不通过时展示的消息;vars是jmeter里面的变量,在java代码中不能通过${aaa}的方式取变量,只能通过vars,当然也可以用vars.put(name,value)进行设置变量;
更多关于jmeter的BeanShell Assertion内容参考https://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Assertion
另外:官网推荐使用 JSR223 Assertion+Groovy
的方式进行自定义脚本编写。
网友评论