The Basics of JMeter Assertions

Casper Henson
GitStacks

--

JMeter is an open source, Java-based application used to check the functional behaviour and performance of an application. Initially, it was designed to test web applications but now it is widely used to test functions. JMeter has various test elements like listeners, assertions, controls, etc. In this article, we are going to explore the foundations of JMeter assertions.

Basics of JMeter Assertions

JMeter load testing is a testing process implemented using Apache JMeter to determine if the web application under the test can satisfy high traffic requirements.

One of the most widely used test plan elements in JMeter is assertions. Assertions are used to compare the expected result with the actual result. As mentioned in the flowchart below, the first step is to add the response to the assertion, which we test against the pattern that we want to validate. Once both inputs are defined, the next step is to add the assertion result to check if the test is successful.

Source

We can use the assertion results by adding the “Assertion Listener” to the group. It will also display the failed assertions in the other listeners. Performance test scripts have assertions defined to verify if the service response is authenticated and there is no impact on the server due to the traffic. The Assertion Listener is also used to perform the functional testing of various applications where we can compare the actual output and the expected output.

Now, let’s look at the list of assertions available with JMeter.

Beanshell Assertion

Beanshell assertion is useful when the test case is unusual and its implementation is difficult. It has access to both internal JMeter APIs and any external classes in the JMeter classpath.

The following components are available in the beanshell enabled components in all JMeter versions:

  • Beanshell sampler
  • Beanshell preprocessor
  • Beanshell postprocessor
  • Beanshell assertion
  • Beanshell function

BSF Assertion

It allows users to write a BSF script to test different scenarios of the sample result.

Compare Assertion

With compare assertion, we can validate if the overall response time of all the samplers which are in the scope of assertions are equal. When we create the test plan, server requests and assertions should be the same. Compare assertion is used for functional testing or mainly for the debugging purpose. We need to provide the following inputs to the compare assertion.

  • Name
  • Comments, if any
  • Compare content
  • Compare time
  • Comparison filters

Response Assertion

It is used to add and compare pattern strings against the various server responses. For example, when we send a request to the URL https://www.facebook.com, we will get a response. We can verify the response using the response assertion.

We can insert “<title>facebook </title>” as a pattern to test field values in the response assertion. Sampler will fail if the response doesn’t contain the string.

We can perform various pattern matching rules to validate the response as below:

  • Contains : regular expression to be matched with the response text
  • Matches : response text matches with the regular expression
  • Equals : response text matches with the pattern, which is a string, not a regular expression
  • Substring : response text contains the pattern, not a regular expression
  • Not : Check if the pattern is not present in the response text

Size Assertion

Size assertion is used to verify the number of bytes in the server response. We can specify equal to, greater than, less than or not equal to a given number of bytes. It is useful when response assertion can’t be used because:

  • The server response is empty
  • The server response time is greater than, lesser than, or equal to a certain number of bytes

Duration Assertion

Duration assertion tests if each server response was received within the given threshold time. In case the response takes more time than the number of given milliseconds, it is marked as a failed response.

XML Assertion

Source

With XML assertion, we can test if the response data consists of the correct XML document. Using the XML assertion, we can validate the format of response in which XML is configured. If the server response is in the XML format, the sampler result is passed. However, if the response is not in the XML format, the result is failed. The main goal of this assertion is just to make sure that the response is in the valid XML format. But it doesn’t validate the response XML based on any schema or document type definition.

HTML Assertion

HTML assertion allows the users to check the HTML syntax of the response data. Response data must match with the HTML syntax. With this type of assertion, the main goal is to validate if the web design and the tags are correct. It scans the web page response with the HTML syntax checker tool and describes the final output in the form of pass or fail. HTML assertion also displays the number of warnings and errors in case the final outcome is failed.

XML Schema Assertion

It is used to validate the response against the specified XML schema. The major difference between the XML assertion and XML schema assertion is that the former only validates if the schema is in the XML format while the latter also checks if the XML schema is correct with reference to the given XML schema. XML schema language is also known as XML schema definition.

XPath Assertion

XPath assertion is used to validate the response using the XPath expression. It validates the response of a specific request with the defined XPath. The assertion result is true if the Xpath is present in the response. It is false if the Xpath is not present in the response.

SMIME Assertion

This assertion is used to validate the body of the MIME message. With this assertion, it mainly checks if the server response received from the email is signed. If the server response is signed then the SMIME assertion result is passed otherwise it is failed.

JSR223 Assertion

It is mainly used to validate the sampler result using the JSR223 scripting. Here, the JSR notation is known as “Java Specification Request”. In JSR assertion, we need to provide the name, comments (if any), the scripting language we want to use, script parameters (if any), the script file containing the actual script, and the manual script in which the assertion logic is defined.

MD5Hex Assertion

It is used to validate the sampler result by checking the MD5Hex hashcode against the hashcode value provided. This assertion creates and calculates the MD5 checksum from the server response and matches the result with the provided MD5HEX code. When the hex calculated from the response is different from the hex in the response then the assertion will fail. Otherwise, the assertion result is passed.

Conclusion

In this article, we explored various types of JMeter assertions. With assertions, we can check the results of our code running against expected results during the runtime itself. This allows us to better understand how our code will perform during periods of high volume.

--

--