Jmeter - Response Assertion To Assert Response Code

Response assertion in jmeter is most important element as it helps you to assert the response of request in your software load test plan. It will show request result failed if response of request is not as expected and show pass if response of request is as expected. So you can verify your software load test result using the response assertion. In this section, We will learn how to assert response code of request using response assertion.

Add Response Assertion In Your software load test plan
You can add assertion under request or parallel to request. To add response assertion under request,
  • Right click on request -> Select Add -> Assertions -> Response Assertion as shown in bellow given image.

It will add response assertion under request.

Example : Assert Response Code Using Response Assertion
I have created very simple example to demonstrate you how response assertion actually works. We will assert response code of software web application's request in this example. If you know, response code 200 represents your HTTP request has succeeded.

In this example, We will assert for response code 200 using response assertion. We will check response code for bellow given 2 requests. One for valid and one for invalid.
  1. newtours.demoaut.com(Valid URL which returns response code : 200)
  2. newtours1.demoaut.com(Invalid URL which returns response code : Non HTTP response code)
I have added 2 HTTP requests under thread group for above URLs as shown in bellow image. 


Also added Response assertion under both requests with same parameter configuration. Here our aim is to assert response code 200 of HTTP request so assertion parameters configuration is as bellow.

Assertion parameters config for both request's response assertion : 
  • Apply To = Main Sample only.
  • Response Field to Test = Response Code.
  • Pattern Matching Rules = Contains.
  • Pattern To Test = 200.


Note : In our example software load test plan, Both response assertions has same above parameter configuration.

Above configuration of response assertion will check response code of HTTP requests. If response code contains 200 then will will be passed. Else it will show result as fail.

I have added Assertion Results listener under both requests as shown in above image to view assertions result on test execution.

Thread group configuration is as bellow.


Now run above software load test plan. Assertion results of both assertions will look like bellow.



Assertion Result of 1st HTTP request : Not display any error in assertion result of of 1st HTTP request. That means HTTP request's response code is 200 as expected.

Assertion Result of 2nd HTTP request : Display error in assertion result of 2nd HTTP request. That means HTTP request's response code is not 200 so assertion is failed.

Also view result tree of both requests will looks like bellow. You can see that response code for 1st request is 200 so it is pass. But response code for 2nd request is Non HTTP response code so it is failed.


This way you can assert response code of any HTTP request of your software load test plan as per your requirement.

No comments:

Post a Comment