Updated March 30, 2023
Definition of JMeter Assertion
The JMeter assertion is used to compute the response of the request which is sent to the server. It is a process where the user can confirm the expected result and achieve the result of the request executed at the run time. So if the user needs to include any assertion on a concerned sampler, then he should add that as a sampler’s child. There are different types of assertions discussed in this article and each has its properties. The configuration and use of JMeter assertion are briefly discussed in this article.
What is JMeter Assertion?
In simple, an assertion in JMeter supports the user to check the server which is under test to produce the expected outcomes. It is a valuable process because the criteria the user configure determine to consider it as a pass and if the values give the expected results, then he can use the same values or criteria as a JMeter variable. There are different types of assertion used in JMeter, they are
• Response assertion
• Duration assertion
• Size assertion
• XML assertion
• HTML assertion
How to use JMeter Assertion?
Assertion can be implied to the main sample and also to its subsamples or at times, it can be inserted into its subsamples only. Few assertions like size or response assertion can be used against the JMeter variable. The code-based assertion like BeanShell, JSR223, BSF doesn’t have a GUI component that identifies the scope. So it signifies that the user should implement all the assertion logic and scope manually. An assertion in JMeter can be applied to all level samples both in a greater and lower scope
So consider the user is having a sample and the assertion is inserted to test the response. The failed assertion can be suspected in two ways in the GUI mode. By using assertion result listener and view result tree listener.
The assertion result listener returns the label where the assertions are taken and the view result tree listener returns all the applied assertions in the test plan. But they are not advised to use at load tests as they consume a huge amount of memory. It should be used for debugging files or opening .jtl files. The failed assertion can be viewed in the error tab in the interface of the load report.
Report fields: The three mandatory fields to be configured are given below.
- Assertion Error: It indicates the true or false option and tells the assertion is succeeded or not. The assertion error is defined as true when there is a problem in assertion like incorrect BeanShell script and sometimes it shows an error if the byte size is not defined. An assertion error can cause affected samples to fail.
- Assertion failure: It is denoted as true or false and tells whether an assertion is a failure or success. If the result of the original assertion matches with the expected result, it is executed as true, or it gives false. If the assertion failure is false, then the affected samplers are considered as failed.
- Assertion failure message string: it is used to custom or build messages which give a brief description of the assertion failure.
JMeter Duration Assertion
The duration assertion assesses that every server response had received within the calculated amount of time. If any response takes a maximum time than the given number of milliseconds it is defined as a failed response. If the user had sent a request to www.gmail.com by JMeter and received a response within the calculated time of 5ms, then the executed test is passed or it is considered as failed.
Response assertion helps to add the pattern strings when compared against the different fields of server response. For example, if the user sends a request to the website http://www.gmail.com and gets back a response from the server. Then the response assertion gives the pattern string as ok and it is considered as pass.
Size assertion checks that each server response comprises the expected number of bytes. The user can specify the size can be equal, not equal, lesser, or greater than the given number of bytes. JMeter sends a request to www.gmail.com and the response packet received, in turn, is a minimum of 5000 bytes if the test is passed, or else it is considered as failed.
The XML assertion checks the response data is comprised of a formal XML document.
HTML assertion enables the user to ensure that the syntax of HTML gives the response to the data. It signifies that the response from the data must match the syntax of HTML.
To use the response assertion, follow the below steps.
The control panel to response assertion can add the pattern strings which is compared against the different field. Add the response assertion from the thread group.
Right-click on the thread group, choose to add, then assertion, and select response assertions.
Then add a pattern to the test. When you transfer the request to the Google server and it may execute a few response codes like 200, 404, and 302. The server error is denoted by 404, server ok is mentioned in 200, and 302 makes the webserver redirect to other pages. It happens when Gmail is accessed from the external USA. Then for Indian users, it is redirected to Gmail. in. choose response code from the response field to test. Choose to add from a panel of response assertion and enter 302 in the new blank entry display. Then add the assertion results from the thread group. Execute the test from assertion result from the thread group. When the user is ready to execute the test choose to run on the menu.
The test result is viewed on the result pane. If the response code from the Gmail server comprises of pattern 302, then the test is passed, or else, the displayed message is, the user can change back from 302 to 500 as the server code doesn’t follow the pattern and if doesn’t follow the pattern again, it is considered as a failed test.
Conclusion
Hence these are the configuration in the JMeter assertion option and the user can implement them in his request to test the server and considered it as successful or not.
Recommended Articles
This is a guide to JMeter Assertion. Here we discuss the definition, What is JMeter assertion?, How to use JMeter assertion, Examples with code implementation. You may also have a look at the following articles to learn more –