jmeter断言
断言是用来判断系统返回的响应结果是否正确,以此帮我们判断测试是否通过。
断言类型很多,这里就介绍几种常用的
1.响应断言
2.大小断言
3.JSON断言
4.持续时间断言
5.BeanShell 断言
响应断言
判断返回内容中的内容是否满足要求。
创建断言
查看返回的内容是否包含开发部员工1
如果结果不包含包含开发部员工1,就会返回红色,说明有问题
大小断言
判断返回的内容是否符合预期大小要求
JSON断言
它只能针对响应结果是applicaton/json格式的请求进行断言
比如我这里配置的是查看是否返回type为success
持续时间断言
对应一些超时判断,判断是否请求在规定时间内获得响应
设置120毫秒获得响应
BeanShell断言
BeanShell断言使用beanshell脚本来执行断言检查,主要用于一些复杂的个性化需求,使用更灵活,功能更强大,但是要能够熟练使用beanshell脚本。beanshell脚本类似java语言但是又不完全是java语言。
内置函数
一些参数介绍
prev.getResponseDataAsString():获取响应信息
prev.getResponseCode() :获取响应code
Failure = false;-----表示断言成功,
Failure = true;-----表示断言失败
FailureMessage = “……”; ----自定义的成功信息,
//获取上一个请求的返回
String jsonString = prev.getResponseDataAsString();
if(jsonString.indexOf("success")==-1){
Failure=true;
FailureMessage = "Return true, and the response code was 失败";
}else{
Failure=false;
FailureMessage = "Return true, and the response code was 成功";
}
if ("200".equals(""+ResponseCode) == false ){
// 响应码不等于200时,设置断言失败,并输出失败信息
Failure=true ;
FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ;
print ( "the return code is " + ResponseCode); // this goes to stdout
log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file
} else {
// 响应码等于200时,设置断言成功,并输出成功信息
Failure=false;
FailureMessage = "Return true, and the response code was " + ResponseCode;
}