Assertions in JMeter with Examples

In this JMeter Tutorial, you will learn about assertions. If we talk in layman language, Assertion is used for checking the response coming from the server and verifying whether the response coming from the server is matching the expected value from the server.

We know that when we send request to the server, it sends response of the request to the client machine. Now in that response, we may want to check whether some expected text is present in the response or not or we may want to check response code, for example, if we have two check whether we are getting 200 response code or not verify and if everything is alright and request is sent and processed properly, then we would use assertion.

Sometimes we mayrequire to check the size and format of the response coming from the server. There may be several other cases where we would required to use Assertion to validate the response coming from the server and checking whether it is as per expectation or not.

In simple words we can say that using Assertion we can check and validate that the data returned by web server as a result of request sent to the web server is correct or not.

Let us see how we would be adding Assertion in JMeter. Process is same. First of all, we will create Thread Group under Test Plan. Then we will add sampler/request and then under that, will be adding Assertion.

Sampler is required to be created first because through Sampler only, we can send the request to the server and get the response from the server accordingly, which would be would be validating through assertion.

Since Assertion is used to validate the response coming from the server, so it is kept under Sampler.

We can see the same in the screenshot given below. Also, in the screenshot we can see types of Sampler available in JMeter.

Assertions in JMeter

In the screenshot above, we can see various types of Assertion available in JMeter. We will be seeing more frequently assertion in JMeter, which are given as below.

Response Assertion

It is used to verify pattern string against various fields of server response. When we send request to the server, it gives response after processing the request and using response assertion we check whether the response contain matching pattern or not.

Let us make it more clear from one example. Suppose we are sending request to the server to open google.com and in the response we have to verify that “<title>Google</title>” which has been entered as a “Pattern to Test” field value in Response Assertion, is available in the response content or not. If the matching pattern is not available in the response, test will fail.

In the screenshot given below, we can clearly see that there is option to select Pattern Matching Rules as well as Field to Test. We can see various options available in Pattern Matching Rules and Field to Test.

We have to select the options available in both the fields, according to our requirement and validate the response accordingly. Let us see the screenshot below to get clearer picture of Response Assertion.

Response Assertion in JMeter

In the following example, I have taken the example of google.com. I have taken server name of google.com in Sampler/ Request.

HTTP Request in JMeter

I have added one Pattern to Test in the response which we can see in the screenshot given below. This test will pass only when we will find the text what has been set in Pattern to Test.

Any content which we have to validate in the response coming from the server, we can use Response Assertion.

Response Assertion in JMeter 2

In View Results Tree, which is a Listener, we can clearly see that test is pass and we have got the Pattern to Test in the response content. We can see the Response Data in the screenshot given below.

We can also see that the test is pass because we have got the content what we expected in the response.

Response Assertion - View Results Tree

Size Assertion

It is simply used to test the size of the response whether it is matching expected size in byte or not. Not only we use equal to comparator but also greater than, less than and not equal to comparator is also used.

We can see in the screenshot given below that we have different types of comparator available. There may be different types of comparator such as Equal to, Greater than, Less than, Greater than or equal to, Less than or equal to etc. We have to use it according to our requirement.

Size Assertion

Duration Assertion

This type of Assertion is used to verify whether the response coming from the server is within given limit of the time set, while configuring Duration Assertion. If the response time is more than the time given/set while configuring Duration Assertion, test fails.

We can see in the screenshot below that we have set the time to 1500 milliseconds. If the response will come after 1500 milliseconds, our test will fail otherwise it will be pass.

Duration Assertion

XML Assertion

XML Assertion is used to verify that after sending the request to the server, we are getting the response in proper XML document format. If the response is not in proper XML format, test will fail.

HTML Assertion

It is almost similar to XML assertion. Using this Assertion, we can verify whether the response coming from the server is in correct HTML syntax or not. In the screenshot below we can see that we are only very fine HTML syntax and nothing else. If HTML response is coming in proper syntax, then test would be pass definitely otherwise it would be fail.

HTML Assertion

Overall, we can say that Assertions are used to validate the response coming from the server. We check whether response is coming as per our expectation or not.

Leave a Comment