jmeter used four kinds of assertions

A, Response Assertion (in response to the assertion)
two, Size Assertion (packet byte size asserted)
three, Duration Assertion (duration asserted)
four, beanshell assertion (assertion free)

 

A, Response Assertion (in response to the assertion)

1. Add the response to the assertion

Web response request to verify the results

 

2. The need to match the input string

Here access to Baidu home page, you need to set the matching string is "Baidu, you know", which returns the text content if contains "Baidu, you know," then even if the Pass

 

Response Assertion configuration parameters

 

Module Type Options Name Configuration instructions
Name   Response Assertion name
Comments   Note
Apply to   Assert range of applications
  Main sample and sub-samples Acting on the parent node and its child nodes sampler sampler
  Main sample only Only the parent node acting sampler
  Sub-samples only It acts only on the sub-sampler node
  Jmeter Variable Name to use Acting Jmeter variable (variable name input box enter the Jmeter)
Field to Test   Field test
  Text Response Matching the text returned from the server in response (excluding Response Headers)
  Response Code Matching the response status code
  Response Message Matching response information. Such as: OK
  Response Headers Match the response of the head
  Request Headers Match request header
  URL Sampled Matching URL link
  Document(text) Match the content of the document
  Ignore Status A number of requests when responding to assertions ignore the result of a response to the claim, and continue to the next assertion
  Request Data Matching the request data
Pattern Mactching Rules   Matching rules
  Contains The results returned include the designated content, supporting regular match
  Matches Match the specified content
  Equals 返回结果与所指定的内容一致
  Substring 返回结果包括所指定结果的字符串,不支持正则匹配
  Not 不进行匹配就算是Pass
  Or 暂不确定该模式的用法
Patterns to Test    
  Patterns to Test 需要匹配的正则表达式、字符串。可以添加多项,每一项会分开进行验证,若某一项验证失败,则其后的不会再进行验证。

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

Guess you like

Origin www.cnblogs.com/georgexu/p/11224095.html