[P46] JMeter Response Assertion (Response Assertion)


1. Response Assertion parameter description

可以对 Jmeter 取样器的响应消息进行检查

使用场景:当响应中有明显的业务标志时,我们可以采用该断言器检测响应报文返回的特征值,进而判断在业务上是否确定

使用频率:非常高,大部分场景均可以使用该断言器

Right click >>> Add >>> Assertion >>> Response Assertion (Response Assertion)

insert image description here

断言成功,查看结果为绿标;断言失败,查看结果为红标

Apply to

  • Main sample and sub-samples: Act on the parent node sampler and the corresponding child node sampler; assert all samplers

  • Main sample only: only acts on the parent node sampler; asserts only on the main sampler

  • Sub-samples only: only applies to sub-node samplers; asserts only on sub-samplers (sub-samplers: original samplers, processed by post-processors, and obtain request results)

  • JMeter Variable Name to use: act on jmeter variables; assert for a certain variable (enter the variable name in the input box)

Field to Test

  • Response text (Text Response): The response text returned from the server, such as body, contains the HTTP header (response data of the request - Response Body)

  • Response Code: such as 200, 404 (Response code in the requested sampler result)

  • Response Message: such as OK (the Response message in the requested sampler result)

  • Response headers (Response Headers): such as Set-Cookie header (response data of the request - Response headers)

  • Request Headers: (Request Request - Request Headers)

  • URL Sample (URL Sample): the address of the request (the request of the request - the address in the Request Body)

  • Document (text) (Document (text)): the text of various types of documents tracked by Apache Tika (for example, the returned HTML format, the text information in it)

  • Ignore Status (lonore Status): Instructs JMeter to set the initial status of the sampler status to success. Whether the sample status is successful is determined by the Response status and the assertion result. When Ignore Status is selected, the Response status is forced to be success, and no further assertion judgment is performed. used only for the first assertion

  • Request Data (Request Data): (Request Request - Request Body)

Pattern Matching Rules¶

  • Contains: the response content includes the content that needs to be matched, which means the response is successful, and supports regular expressions

  • Matches: If the response content completely matches the content that needs to be matched, it means that the response is successful. It is case insensitive and supports regular expressions.

  • Equals: The response content must be completely equal to the content to be matched to indicate success. It is case-sensitive. The content to be matched is a string regular expression

  • Substring: The return result contains a string of the specified result, but subString does not support regular strings

  • No (Not): do not match

Test Patterns (Patterns Matching Rules):Used with pattern matching rules, you can write values ​​directly or use regular expressions

Custom failure message:custom failure message

2. Preparation

Mumu Fresh: http://111.231.103.117/#/login

  • After entering the webpage, log in, and the page provides the account and password

  • Type "shrimp" in the search box

  • Right-click to check or press F12 to open the debugging tool and click Search

insert image description here

3. Test plan design

3.1, including

(1), right click on the test plan <<< add <<< configuration original <<< user-defined variables

insert image description here

  • Add a variable: IP, the value is 111.231.103.117

(2), right click on the test plan <<< add <<< test fragment <<< test fragment

insert image description here

  • Name: HTTP Sample Request

(3) On the web page, click on the network during debugging, find the request, click on the load, and copy the parameter value

insert image description here

(4), HTTP sampling request right click <<< add <<< sampler <<< HTTP request

insert image description here

  • Protocol: GET

  • Path: /product/list

  • Click Add from Clipboard, it will be added automatically; you can also add it manually

(5), HTTP request right click <<< add <<< post processor <<< JSON extractor

insert image description here

  • Tick ​​Main sample only

只对主取样器进行提取

  • Name of created variable:var1

参数名为:var1

  • JSON Path expressions:$…list[*].name

$:开始符固定的;..:0到多个级别,单个为根级别;list:返回的响应数据结构,相当于字典,key为list的值;name:list值中名为name键的值

  • Match No. (0 for Random):-1

匹配所有符合正则表达式的值

(6), HTTP request right click <<< add <<< post processor <<< debug post processor

insert image description here

  • The parameters are default

(7), HTTP request right click <<< add <<< configuration element <<< HTTP request default value

insert image description here

  • protocol: http

  • Server name or IP: ${IP}

使用变量获取ip

  • Port: 8083

  • Content-Encoding: utf-8

(8) On the web page, click on the network during debugging, find the request, click on the header to find the request header, and copy all the request headers

insert image description here

(9), right click on HTTP request <<< add <<< configuration element <<< HTTP header manager

insert image description here

  • Click Add from clipboard, the copied value will be added automatically

如图,可以将信息头中的 ip 使用变量 ${IP} 代替

(10), right click on the test plan <<< add <<< thread (user) <<< thread group

insert image description here

  • The parameters are default

(11), right click on the thread group <<< add <<< logic controller <<< module controller

insert image description here

  • Select HTTP sampling request

(12), right click on thread group <<< add <<< assertion <<< response assertion

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • Tick ​​to include

  • In the test mode, click Add and enter the regular expression: .*prawn

表示断言响应文本中,是否包含 “虾” 这个字;.*代表任意字符;测试模式中可以直接写值也可以使用正则表达式;比如:龙虾、.*虾、.*虾.*

(13), right click on thread group <<< add <<< listener <<< view result tree

(14), click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,HTTP请求成功,HTTP请求的响应数据下的Response Body中存在多个 “虾” 字,所以断言成功,请求也是成功的

Include: As long as the selected test field contains the regular expression of the added test pattern, the assertion is successful

3.2. Matching

The test plan design is modified and verified on the basis of 3.1

(1), modify the response assertion, check the match

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • check match

  • In the test mode, click Add and enter the regular expression: .*prawn

表示断言响应文本中,是否匹配所设置的正则表达式;.*代表任意字符

(2), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,主取样器 HTTP请求 失败,说明断言是失败的,因为勾选的是 Main sample only,并不断言子取样器,所以子取样器 HTTP请求-0 是成功的

(3), Modify the response assertion, change the regular expression in the test mode to: .*shrimp.*

insert image description here

.*虾.*

(4), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,此时请求成功,说明断言也是成功的

Match: If the selected test field matches the regular expression in the added test pattern (all values ​​in the test field match), the assertion is successful

3.3 Equal

The test plan design is modified and verified on the basis of 3.2

(1), modify the response assertion, check equal

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • Tick ​​equal

  • In the test mode, click Add, and copy and paste all the return values ​​under the Response Body under the response data under the HTTP request

选择 相等时,不能写正则表达式

(2), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,此时请求成功,说明断言也是成功的

Equal: In the selected test field, the value in the test mode must be exactly the same as the value returned by the selected test field, then the assertion is successful; when equal is selected, the regular expression in the test mode is invalid

3.4, String

The test plan design is modified and verified on the basis of 3.3

(1), modify the response assertion, check the string

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • Check the string

  • In the test mode, click Add to add the string that needs to be asserted: Lobster

只要包含多添加的字符串即断言成功;选择 字符串时,不能写正则表达式

(2), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,此时请求成功,说明断言也是成功的

String: If the selected test field contains a string value in the test pattern, the assertion is successful; when a string is selected, the regular expression in the test pattern is invalid, and the written regular expression will be judged as a string

3.5, string

The test plan design is modified and verified on the basis of 3.4

(1), Modify the response assertion, check No

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • Check the string

  • In the test mode, click Add to add the string that needs to be asserted: Lobster

只要包含多添加的字符串即断言成功;选择 字符串时,不能写正则表达式

  • check no

取反值,当字符串断言成功时,则取反,断言失败,请求失败;当字符串断言失败时,则取反,断言成功,请求成功

(2), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,主取样器 HTTP请求 失败,说明断言是失败的;3.4 中断言是成功的,响应断言 中勾选 否,则取反,断言失败,请求失败

No: negate the value, when the selected test field corresponds to the written test pattern, negate it, the assertion fails, and the request fails; when the selected test field does not correspond to the written test pattern, negate it, assert success, the request was successful

3.6, or

The test plan design is modified and verified on the basis of 3.5

(1), modify the response assertion

insert image description here

  • Tick ​​Main sample only

只对主取样器(HTTP请求)进行断言

  • Tick ​​response text

请求的响应数据——Response Body

insert image description here

  • Check the string

  • In the test mode, click Add to add the string that needs to be asserted: lobster, crayfish, Australian lobster

测试模式中可以添加多个需要判断的字符串,只有都包含匹配上,才断言成功

(2), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,主取样器 HTTP请求 失败,说明断言是失败的;HTTP请求,响应数据下的Response Body中,并不存在 ”小龙虾“ 字符串,所以断言失败

(3), modify the response assertion, check or

insert image description here

  • check or

只要测试模式中的字符串有一个匹配上,则断言成功

(4), click to clear all, click to start, click to view the result tree, view the request and sampler information

insert image description here

如图,此时请求成功,说明断言也是成功的

Or: when the selected test field corresponds to one of the written test pattern values ​​in the written test pattern, the assertion is successful

Guess you like

Origin blog.csdn.net/qq_45138120/article/details/130977085