How to use Assertions in JMeter? Types of Assertions, When to use, Practical Exercise

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.JMeter_Assertions_Tutorial

Response Assertion

If you want to compare various fields of the server response to pattern string, you can use Response Assertion.JMeter_Response_Assertions_Tutorial

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,

Duration Assertion

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.JMeter_Duration_Assertions_Tutorial_Settings

For example, in above setting, the maximum number of milliseconds allowed for each response is 100ms.

Size Assertion

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.JMeter_Size_Assertions_Tutorial

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

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.JMeter_XML_Assertions_Tutorial

HTML Assertion

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.JMeter_HTML_Assertions_Tutorial

Practical hands on exercise with Assertion in JMeter

Let take up a practice exercise with Duration Assertion. Here is the roadmap of this exercise.JMeter_Duration_Assertions_Tutorial

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.JMeter_Duration_Assertions_Tutorial_Step1

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 AssertionJMeter_Duration_Assertions_Tutorial_Step2_Training

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.JMeter_Duration_Assertions_Tutorial_Step3_Training

Step 3. Add Assertion Results

Right click on Thread Group and choose Add > Listener > Assertion ResultsJMeter_Duration_Assertions_Tutorial_Step4_Training

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.JMeter_Duration_Assertions_Tutorial_Step5_Training

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.JMeter_Duration_Assertions_Tutorial_Step6_Training

Now click the Start button to run test plan, you will see the error message when the test fails and the test will stop.JMeter_Duration_Assertions_Tutorial_Step7_Training

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.