Jmeter/性能测试从入门到进阶

12. Jmeter-断言

2019-09-23  本文已影响0人  软件测试汪

jmeter-断言介绍与使用

性能测试中较少用到断言。断言会增加脚本执行时间,但是接口测试中断言是必备的。什么是断言?其实就是功能测试中常说的预期结果和实际结果是否相等。

image

响应断言

image.png

见图知意。接口测试中较少用到。

JSON Assertion

image.png

也算是见图知意吧。这里需要注意一下
Additionally assert value: 等于特定值
Match as regular expression:符合正则表达式

Size Assertion

image.png

见图知意。。。

JSR223 Assertion

image.png
JSR223 Assertion可以选择多种语言,如下图所示:
image.png
笔者常用的是beanshell和JS。这里我们以beanshell举例。
JSR223 Assertion实例
image.png
image.png
image.png
import org.json.*;

//获取上一个请求的返回值
String response = prev.getResponseDataAsString();
//将返回值转换为json
JSONObject responseJson = new JSONObject(response);
//获取responseMessage
String status = responseJson.get("status").toString();
String message = responseJson.getString("message");



if(!status.equals("0")) {
    Failure = true; 
    FailureMessage = "status错误";
}else{
    FailureMessage = "status正确";
    }

if(!message.equals("username or password error")) {
    Failure = true; 
    FailureMessage = "message错误";
}else{
    FailureMessage = "message错误";
    }

运行结果

image.png

XPath Assertion

image.png
可以参考11. Jmeter-后置处理器二中的XPath Extractor

Compare Assertion

image.png

嗯。。用到正则了。暂时跳过。

HTML Assertion

image.png

MD5Hex断言

image.png

对返回的MD5进行断言,直接填入即可。

SMIME Assertion

image.png
可以参考SMIME Assertion,用的不多。

XML断言

image.png

用来验证响应数据是正确的XML格式,不常用。

XML Schema Assertion

image.png

BeanShell断言

可以参考JSR223 AssertionBeanShell断言也是我们常用的断言。

软件测试汪简书地址
软件测试汪博客地址

欢迎关注微信公众号:软件测试汪。软件测试交流群:809111560

转载请注意出处,谢谢合作

上一篇 下一篇

猜你喜欢

热点阅读