断言介绍与使用
性能测试中较少用到断言。断言会增加脚本执行时间,但是接口测试中断言是必备的。什么是断言?其实就是功能测试中常说的预期结果和实际结果是否相等。
image
响应断言
JSONAssertion
SizeAssertion
JSRAssertion
XPathAssertion
CompareAssertion
断言持续时间
HTMLAssertion
MD5Hex断言
SMIMEAssertion
XML断言
XMLSchemaAssertion
BeanShell断言
1、响应断言image.png
见图知意。接口测试中较少用到
2、JSONAssertionimage.png
也算是见图知意吧。这里需要注意一下
Additionallyassertvalue:等于特定值
Matchasregularexpression:符合正则表达式
3、SizeAssertion(见图知意)image.png
4、JSRAssertionimage.png
JSRAssertion可以选择多种语言,如下图所示:
image.png
笔者常用的是beanshell和JS。这里我们以beanshell举例。
JSRAssertion实例:
image.png
image.png
image.png
importorg.json.*;
//获取上一个请求的返回值
Stringresponse=prev.getResponseDataAsString();
//将返回值转换为json
JSONObjectresponseJson=newJSONObject(response);
//获取responseMessage
Stringstatus=responseJson.get("status").toString();
Stringmessage=responseJson.getString("message");
if(!status.equals("0")){
Failure=true;
FailureMessage="status错误";
}else{
FailureMessage="status正确";
}
if(!message.equals("usernameorpassworderror")){Failure=true;
FailureMessage="message错误";
}else{
FailureMessage="message错误";
}
运行结果:
image.png
5、XPathAssertion
image.png
6、CompareAssertion
image.png
7、断言持续时间
image.png
这个就比较好理解了。断言取样器运行测试消耗掉的时间,比如我们的持续时间填写10,实际时间如果是20s,那么就会断言失败。
8、HTMLAssertion
image.png
9、MD5Hex断言
image.png
对返回的MD5进行断言,直接填入即可。
10、SMIMEAssertion
image.png
可以参考SMIMEAssertion,用的不多。
11、XML断言
image.png
用来验证响应数据是正确的XML格式,不常用。
12、XMLSchemaAssertion
image.png
13、BeanShell断言
可以参考JSRAssertion,BeanShell断言也是我们常用的断言。
文末福利: