Assertions in JMeter – JMeter Tutorials Series Day 7

In previous article we have seen What all types of Timers supported in JMeter & what is meaning of each timer? However if you adding timer for reason where expected to load some value with the specified time & specified value is getting properly as per mentioned time then timer will work for your test cases. But sometimes due some reason the expected value is not received with specified time frame then your test cases execution will fail. Under such circumstances you should thing of using different types of Assertions in JMeter.

Assertion or verification plays an important role in testing. It helps to verify and ensure that testing process is going in a right direction and the server which is under test returns the expected results. For example; when user sends requests to the server and wait for the response, after a long wait when user gets response then how user will be ensure that the coming response from the server is the correct one. To resolve this issue, use assertion or verification to ensure that the response comes from server at run time is correct, if the response will be not correct then test will get fail for that request.

These are some types of Assertion used in JMeter,

  • Response Assertion
  • Duration Assertion
  • Size Assertion
  • XML Assertion
  • HTML Assertion

 

Response Assertion

The Response assertion facilitates users to add pattern strings to be compared against several areas of the server response.

For example, if somebody sends request to the website http://www.Google.com and wait for the server response. In this case, Response Assertion role is to verify that the server response has probable pattern string, e.g. “OK” or not.

Jmeter Add Response Assertion

 

Duration Assertion

The role Duration Assertion in JMeter to test that each response comes from server has received within a given amount of time or not. If any server response takes longer time than the given number of milliseconds specified by the user then the response gets fail.

Jmeter Duration Assertion

 

For example, , if somebody sends request to the website http://www.Google.com by JMeter and receives a response within limited time 5 ms then test case pass, else test case fails.

 

Size Assertion

The role of Size Assertion in JMeter to test that each response comes from server holds the expected number of byte. Size Assertion facilitates users to specify the size i.e, equal to, greater than, less than or not equal to a given number of bytes.

For example, , if somebody sends request to the website http://www.Google.com by JMeter and receives response packet with less than expected byte 5000 bytes in size then a test case pass, else a test case fails.

JMeter Size Assertion

 

XML Assertion

The role XML Assertion in JMeter to test that each response comes from server holds the data in correct XML document.

 

HTML Assertion

The role of HTML Assertion in JMeter is to permit users to check the HTML syntax of the response data. That means, the response data should contain the HTML syntax.

JMeter HTML Assertion

 

How to use Assertion?

Here we will take an example of Response Assertion to test and compare the response packet comes from the server “http://www.Google.com” matches your expected string. Also, the response assertion facilitates users to add pattern strings to be compared against various fields of the response sends by server.

To test this, we will proceed in the following manner,

First: Add Response Assertion

Second: Add Pattern

Third: Add Response Assertion result

Fourth: Run Test Result

 

First: Add Response Assertion

After opening JMeter interface, right-Click on Thread Group -> Add -> Assertions -> Response Assertion

JMeter Add Response Assertion

 

Given below figure displays the Response Assertion Pane,

JMeter Response Assertion

 

Second:  Add Pattern

When user sends request to the Google server, it sends response with some response code,these codes explains the status of the server,some common response codes are,

200 – The server successfully sent the response

302–Generally it happens that, when user access www.google.com from outside USA then the Web server redirect to other page. That means; URL of the Google re-directs to country specific website page URL. As shown below, www.google.com redirects to www,google.co.in for Indian Users.

404 – Server error because request doesn’t exist

503 – The server is temporarily unavailable

Let us verify the responses code pattern 302 of the Google.com web server,

To check the responses code pattern 302, In Response Assertion pane, go to “Response Field To Test” and choose “Response Code”, then click on “Add”, a new text box displays below the “Pattern to Test” field. In”Pattern to Test” text box enter 302.

JMeter Add Pattern to test

 

Third: Add Response Assertion result

From left side on the pane of Thread Group, right click on Thread Group, and from the context menu click on Add -> Listener -> Assertion Results.

Response Assertion result pane will display like this,

 

JMeter Assertion result pane

 

Fourth: Run Test Result

After opening Assertion Result pane, click on Run button from the menu bar, or from the keyboard press Ctrl+R will display the test result on Assertion Results pane. If Google server response code pattern is 302, the test case is passed and the result window displays shown in the figure below

JMeter Run Test Result

 

Conclusion:

In today JMeter Tutorials Series Day 7, we have seen what all Assertions used in JMeter. The list might not seem complete to you and readers are always welcomed to suggest their views and add opinions.

Happy Testing!!!

 

3 thoughts on “Assertions in JMeter – JMeter Tutorials Series Day 7”

Leave a Comment

Share This Post