zoukankan      html  css  js  c++  java
  • jmeter常用四种断言

    jmeter常用四种断言

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

    一、Response Assertion(响应断言)

    1. 添加响应断言

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

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

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

     

    Response Assertion配置参数

    模块类型选项名称配置说明
    Name   Response Assertion名称
    Comments   注释
    Apply to   断言应用的范围
      Main sample and sub-samples 作用于父节点取样器及其子节点取样器
      Main sample only 仅作用于父节点取样器
      Sub-samples only 仅作用于子节点取样器
      Jmeter Variable Name to use 作用于Jmeter变量(输入框中可输入Jmeter的变量名称)
    Field to Test   测试的字段
      Text Response 匹配从服务器返回的响应文本(不包括Response Headers)
      Response Code 匹配响应状态码
      Response Message 匹配响应信息。如:OK
      Response Headers 匹配响应头
      Request Headers 匹配请求头
      URL Sampled 匹配URL链接
      Document(text) 匹配文档内容
      Ignore Status 一个请求多项响应断言时,忽略某一项断言的响应结果,而继续下一项断言
      Request Data 匹配请求数据
    Pattern Mactching Rules   匹配的规则
      Contains 返回的结果包括所指定的内容,支持正则匹配
      Matches 根据指定内容进行匹配
      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;
         }
    }
  • 相关阅读:
    left join问题
    SQL索引
    数据库查询优化
    define and inline
    程序的内存分配
    __closure
    this指针
    java笔记
    Visual Studio Code(VSCODE)语言设置
    Excel 2010如何打开多个独立窗口?
  • 原文地址:https://www.cnblogs.com/111testing/p/11219979.html
Copyright © 2011-2022 走看看