Jmeter常用断言

一、Response Assertion(响应断言)
二、Size Assertion(数据包字节大小断言)
三、Duration Assertion(持续时间断言)
四、beanshell 断言(自由断言)

一、Response Assertion(响应断言)

1. 添加响应断言

对Web请求的响应结果进行验证

2. 输入需要匹配的字符串

此处对于访问Baidu首页,需要设置匹配的字符串为“百度一下,你就知道”,表示返回的文本内容若包含有“百度一下,你就知道”,则就算Pass

Response Assertion配置参数

 

3. 添加:断言结果(Assertion Results)、查看结果树(View Results Tree)

4. 运行Test Plan中的线程组,进行断言检查

以下可观察到响应数据中是包含所指定的验证字符串,Pass

二、Size Assertion(数据包字节大小断言)

 判断响应结果是否包含正确数量的byte。可定义(=, !=, >, <, >=, <=)

三、Duration Assertion(持续时间断言)

 判断是否在给定的时间内返回响应结果

四、beanshell 断言

Bean Shell常用内置变量

   JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下:

log:写入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);

ctx:该变量引用了当前线程的上下文,使用方法可参考:org.apache.jmeter.threads.JMeterContext。

vars - (JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

    a) vars.get(String key):从jmeter中获得变量值

    b) vars.put(String key,String value):数据存到jmeter变量中

    更多方法可参考:org.apache.jmeter.threads.JMeterVariables

props - (JMeterProperties - class java.util.Properties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值,而不能是一个对象。对应于java.util.Properties。

    a) props.get("START.HMS");  注:START.HMS为属性名,在文件jmeter.properties中定义

    b) props.put("PROP1","1234");

prev - (SampleResult):获取前面的sample返回的信息,常用方法:

    a) getResponseDataAsString():获取响应信息

    b) getResponseCode() :获取响应code

    更多方法可参考:org.apache.jmeter.samplers.SampleResult

sampler - (Sampler):gives access to the current sampler

 在这里除了可以使用beanshell的内置变量外,主要通过 Failure 和 FailureMessage来设置断言结果。

其中脚本内容如下:

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;
     }
}

  

原文:https://www.cnblogs.com/111testing/p/11219979.html



原文地址:https://www.cnblogs.com/sucretan2010/p/14830469.html