I. Introduction assertion
There is a known element jmeter assertion (Assertion). For checking whether the test response data obtained in line with expectations for performance testing process to ensure data exchange in line with expectations.
The purpose of using assertions: add a layer to determine the mechanism of the return level request; because the request is successful, the result does not mean necessarily correct.
Use assertions:
△ added at a corresponding selected Sampler asserted (because of the different assertion checking the contents of different types); good response to check the contents of the configuration (according to the circumstances asserted, asserts some control panel without adding any content, such as XML Assertion) .
△ add an assertion the result listener (add the listener), through the "assertion results" to see if by assertions; for a request, if passed, will only result assertion request print the name of his party;
If it fails, in addition to the name of the request, there will be reasons for the failure of his party (different types of assertions, different results).
PS: a Sampler can add multiple assertions, to add the appropriate assertions based on your inspection needs, when all assertions under Sampler are passed, then the request is considered successful.
Second, the type of assertion
There are 12 bell assertion Jmeter4.0
Third, the introduction of some common assertions
1, in response to the assertion
Add the following in response to the assertion in the sampler
It applies to: Scope
Main sample and sub-samples: acting on the corresponding parent node and a child node sampler sampler
Main sample only: to act only on the parent node sampler
Sub-samples only: applied only to a child node sampler
JMeter Variable: acting jmeter variable (variable name input box enter the jmeter)
To test the response field: item to be checked
Response text
Response Code
Response information
Response Headers: response header
Request Herders: Request header
URL sample
Documeng(text)
Ignore status: ignore the return status code of the response message,
Request Datas
Pattern matching rules:
Includes: return results include content you specify
:( seem to match with Equals little investigation, I could not understand what is the difference)
Equals: Returns the specified result consistent results with you
Substring: the result is the result of the specified string
No: Does not match
or
To test the model: that is to fill the result you specify (can fill multiple ), the buttons [Add], [Delete] is designated content management
2, XPath assertions
XPath for the XML Path Language, which is a method for determining the XML (a subset of the Standard Generalized Markup Language) document in a language part position. XPath-based XML tree structure, providing the ability to find a node in the data tree.
Target: for the return data type information for the assertion of XPAth. If the server response is returned content xml format, then the best type of authentication assertion is to use XPath Assertion.
Apply to: Scope
Main sample and sub-samples: the main sample and subsample
Main sample only: only the main sample
Sub-samples only: only subsample
JMeter Variable: jmeter variable (input box you can enter the name of the variable jmeter)
Options Parsing XML: XML parsing options
Use Tidy (tolerant parser): Use Tidy (resolver fault-tolerant), the default selection quiet (not shown)
Quiet: do not show
Report errors: Error Reporting
Show warnings: error
Use Namespaces: namespace
Validate XML: validate XML (package / data)
Ignore Whitespace: Spaces are ignored ( this allows you to specify the parser can ignore what the space, and the space which is important)
Fetch external DTDs: access to external DTDs (some XML elements with attributes, attribute contains information used by the application, only the property of the elements in the program read, write, provide additional information elements, this time in the need to declare in DTDs)
XPath Assertion: written in the input box xpath assertions, click Validate to verify its correctness
True if nothing matches: confirmation do not match
3, size assertions
For determining the size of the content to return, in response to determination result contains the correct number of byte. Define (=,! =,>, <,> =, <=)
APPly to: Applications (returns the contents of assertions range)
Main sample and sub-samples:作用于父节点取样器及对应子节点取样器
Main sample only:仅作用于父节点取样器
Sub-samples only:仅作用于子节点取样器
JMeter Variable:作用于jmeter变量(输入框内可输入jmeter的变量名称)
Response Size Field to Test:响应字节的测试范围(可以选择用于判断的响应范围)
Full Response:全部响应
Response Headers:响应头部
Response Body:响应主体
响应代码:响应报文相关的代码
响应信息:响应报文的信息
Size to Assert:断言字节范围
字节大小单位为:字节;比较顺序是①返回内容的大小②比较类型③指定字节大小
4、断言持续时间
作用:用于判断服务器的响应时间,判断是否在给定的时间内返回响应结果
APPly to:适用范围
Main sample and sub-samples:作用于父节点取样器及对应子节点取样器
Main sample only:仅作用于父节点取样器
Sub-samples only:仅作用于子节点取样器
持续时间(毫秒):响应时间设置(单位:毫秒),如果响应时间大于设置的响应时间,则断言失败,否则成功
5、BeanShell断言
(1)BeanShell断言介绍
BeanShell是jmeter的解释型脚本语言,和java语法大同小异,并有自己的内置对象和方法可供使用。 BeanShell断言可以使用beanshell脚本来执行断言检查,可以用于更复杂的个性化需求,使用更灵活,功能更强大,但是要能够熟练使用beanshell脚本。
作用对象:针对sampler中的Bean Shell sampler而使用的断言
Name名称:断言的名字(可以用一个比较容易理解和分辨的名称)
Comments注释:对这个断言进行一个解释,备注
Reset bsh.interpreter before each call:在每次调用Bean Shell之前重置bsh.interpreter类(bsh.interpreter是Bean Shell脚本语言的一种类,也可以理解为一种解析器)
Parameters参数(String Parameters and String []bsh.args):String参数(String []bsh.args是主类main函数的形式参数,是一个String 对象数组,可以用来获取命令行用户输入进去的参数)
Script file脚本文件:可以填入脚本文件路径
Script(see below for variables that are defined):参照下文定义的变量(使脚本文件参照定义的变量来运行)
(2)BeanShell断言实例
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来设置断言结果。
简单实例:
断言结果: