Assertions in JMeter provide the criteria to determine whether the test case has passed or failed. You can use JMeter Assertions to ensure returned values match expected results. They are processed after every sampler in their scope.
Some common Assertions in JMeter are listed below.
If you want to compare various fields of the server response to pattern string, you can use Response Assertion.
Example: If your application returns HTML data, you can use Assertions to verify the data contains an expected string, for example “<!– Page rendered successfully –>” in the rendered page,
The Duration Assertion verifies that each response was received within a given amount of time. If there is any response that takes longer than that time, JMeter marks it as a failed response.
For example, in above setting, the maximum number of milliseconds allowed for each response is 100ms.
The Size Assertion tests to verify that each response contains the right number of bytes in it. JMeter allows you to set the size equal, greater, less or not equal to the specified value.
In above setting, if the response returned from the server is less than or equal to 2048 bytes, the test case passes otherwise the test case fails.
XML Assertion verifies that the response data consists of a formally correct XML document. There are no configuration option other than the Name and the Comments field in this element.
If you want to check whether the HTML syntax of the response data is correct or not, you can use the HTML Assertion. If the response data meet the specified HTML syntax it passes, otherwise it fails.
Practical hands on exercise with Assertion in JMeter
Let take up a practice exercise with Duration Assertion. Here is the roadmap of this exercise.
Step 1. Add HTTP request
Start JMeter. As explained in previous topics, add a Thread Group to the Test Plan. Add HTTP Request sampler with the Server set to www.google.com
Configure the Thread Group setting as shown below.
The test will stop if there is a test case failure.
Step 2. Add Duration Assertion
Right click on Thread Group and choose Add > Assertions > Duration Assertion
Configure the Assertion setting as shown below. In this case if any response returns from server within 2000ms, the test cases pass otherwise test cases will fail.
Step 3. Add Assertion Results
Right click on Thread Group and choose Add > Listener > Assertion Results
The Assertion Result listener will display the test result of the assertion when executing the test.
Step 4. Run the test
Click Run button on the menu bar to run the test. You will see the test execute as below. If the response time from server is within 2000ms, you will the message shown in the screenshot below.
Now go back to the Duration Assertion and change the Duration in millisecond setting to 100ms. This means the test case will pass if the response time is within 100ms otherwise it fail.
Now click the Start button to run test plan, you will see the error message when the test fails and the test will stop.
You might also see a similar error message in the first case if the duration exceeds 2000ms (2 seconds).
This topic includes the sample JMeter Assertion Test Plan which you can download and execute on your machine.
In the next topic we will discuss how to execute Distributed Testing in JMeter.