What is the core purpose of Testing any Software? To make sure that everything is working as supposed to be and there is no deviation from actual expectation. Right? When testing any software or web application manually, you compare the actual result with expected behavior and capture the result in form of Pass or Fail. When doing performance testing with Jmeter, listeners will give you all green indicating success of your samples. If you look at the response in listener View Result Tree, you will notice that server has not responded with valid data. Hence, it’s a failure. To avoid such unexpected failures, Jmeter provides a very important feature: Assertions.
Assertions play a very key role in software testing, as it helps to confirm that server has responded with the expected result. You can define Pass/Fail conditions at any level of you Jmeter Test Plan. Jmeter provides us variety of Assertions for different purpose. First, let’s have a look at list of Assertions we can have in Jmeter:
1) BeanShell Assertion: To allow user to perform assertion checking using BeanShell script.
2) Compare Assertion: The Compare Assertion is used to compare sample results within its scope. Either the contents or the elapsed time can be compared, and the contents can be filtered before comparison.
3) Duration Assertion: The Duration Assertion checks that each response was received within a specified amount of time. Any response that takes longer than the given number of milliseconds (specified by the user) is marked as a failed response.
4) HTML Assertion: The HTML Assertion allows the user to check the HTML syntax of the response data using JTidy.
5) JSR223 Assertion: The JSR223 Assertion allows use of JSR223 script code to check the status of the previous sample.
6) MD5Hex Assertion: The MD5Hex Assertion allows the user to verify the MD5 hash of the response data.
7) Response Assertion: You can add pattern strings to compare against any field of request of response. Response Assertion control panel contains matching rules as: “Contains”, “Matches”, “Equals” & “Substring”. You can apply assertion to fields: “Text Response”, “Response Code”, “Response Message”, “Response Headers”, “Request Headers”, “URL Sampled”, “Document(text)”, “Ignore Status”.
8) Size Assertion: The Size Assertion tests that each response contains the right number of bytes in it. You can specify that the size be equal to, greater than, less than, or not equal to a given number of bytes.
9) SMIME Assertion: Purpose of the SMIME Assertion is to evaluate the sample results from the Mail Reader Sampler. This assertion verifies if the body of a mime message is signed or not. Also, we can verify the signature against a specific signer certificate.
10) XML Assertion: This Assertion make sure that the response data consists of a formally correct XML document. It does not validate the XML based on a DTD or schema or do any further validation.
11) XML Schema Assertion: If you want to validate a sample response against an XML Schema, use this assertion.
12) XPath Assertion: The XPath Assertion tests a document for well formedness, has the option of validating against a DTD, or putting the document through JTidy and testing for an XPath. If that XPath exists, the Assertion is true. Using “/” will match any well-formed document, and is the default XPath Expression.
How to Add Assertions in Jmeter?
Well, adding Assertion is very easy. Assertion can be added to any controller in a Test Plan, main samples or sub samples. We can also add multiple assertions to Test Plan, controller or Thread Group. Follow below steps to add an Assertion:
Right Click > Add > Assertions > Select Assertion.
As you can see, Assertions are very important to make sure you are getting proper valid response. The most used one is Response Assertion. Make sure you are using the appropriate one for your requirement. Performance Test Plan is incomplete without proper use of Assertions.