JMeter Assertion

发布时间 2023-03-24 14:33:13作者: 小冰钓鱼

 

Response Assertion  (响应断言)

用于判断接口请求的响应结果是否符合预期的一种断言方式。

 

Apply to:

  • Main Samples and Sub Samples: JMeter will check for the 'expected text' to be present in both main and sub samples and if any of the samplers do no have the text, it fails.
  • Main Sample Only: - JMeter will check for the 'expected text' only in the main sample. It fails if it is not found.
  • Sub Sample only: - JMeter will check for the 'expected text' only in all the sub samples. All the sub samplers are expected to have the text. Otherwise it fails.
  • Main sample and sub-samples:匹配范围为当前主请求,及子请求;
  • Main sample only :仅当前主请求;
  • Sub samples only:仅子请求;
  • JMeter Variable:变量值进行匹配;
  • 关于应用范围,我们大多数勾选“main sample only” 就足够了,因为我们一个请求,实质上只有一个请求。但是当我们发一个请求时,可以触发多个服务器请求,类似于ajax那种,那么就有main sample  和 sub-sample之分了。
    此外,对于有重定向的请求,并且勾选了“跟随重定向”, 那么这两个请求都是 sub-sample,重定向后的请求(第二个请求)就是main-sample。

Field to Test:

  • Text Response  响应文本:响应服务器返回的文本内容,http协议排除header部分;
  • Response Code 响应代码:匹配响应代码,比如http请求中‘200’代表成功;
  • Response Message  响应信息:匹配响应信息,处理成功返回‘成功’或者“ok”字样;
  • Response Header 响应头 :匹配响应头中的信息;
  • Request Headers 请求头 ;
  • URL Sampled :是对sample的url进行断言。如果请求没有重定向(302),那么就是这个就是请求url。 如果有重定向(且跟随重定向),那么url就包含请求url 和 重定向url;
  • Document(Text):一切Apache Tika 支持服务器响应,包括文本响应,还支持 PDF, Office, Audio, Video formats。jmeter会用Apache Tika 去解析服务器响应内容,会很耗内存,而且也很容易解析失败。所以一般普通http请求,不要选择这个;
  • Ignore Status:当我们要验证404,501等http响应代码时,需要勾选“ ignore status”。因为当http 响应代码为400,500时,jmeter默认这个请求时失败的;
  • Request Data:请求数据

Patterns Matching Rules:

  • Contains:响应内容包括需要匹配的内容就算成功;
  • Matches:响应内容要完全匹配匹配内容,不区分大小写;
  • equals:完全相等,区分大小写;
  • substring:响应内容包括匹配内容即为成功。
  • Not:勾选上之后,会对前面选择的进行反转。比如Matches + Not 就是不匹配就算Pass;
  • Or :添加多个匹配内容时,会用到。

Patterns to Test:匹配内容

Custom failure message:当断言判断当前请求失败时,显示信息。

 

Duration Assertion(响应时间断言)

是一种响应时长的断言

Apply to:

  • Main Samples and Sub Samples: JMeter will check for the 'expected text' to be present in both main and sub samples and if any of the samplers do no have the text, it fails.
  • Main Sample Only: - JMeter will check for the 'expected text' only in the main sample. It fails if it is not found.
  • Sub Sample only: - JMeter will check for the 'expected text' only in all the sub samples. All the sub samplers are expected to have the text. Otherwise it fails.

Duration to Assert:

Duration in milliseconds:响应时长,单位ms,超过设定的时间时,判定当前请求失败。