Sunday, August 16, 2020

Jmeter Assertions - Response Assertion

Assertion in JMeter is used to validate response of the request that you have sent to the server.

HTTP request/response 

In the above picture, Request (contains Method, Resources, Headers and Content) is sent from client machine and processed by server machine and sent back the Response (Contains Status code, Headers and Content) to client machine.

JMeter assertion is used to validate the Response (Either Response code or Content) with the expected result (You must know the expected result).

When we are talking about assertion, we must know to how to add assertion in the JMeter test plan.

Remember few points when you are working with JMeter assertions.

  • Assertion is always executed after your sampler and post processor executed.
  • Assertions apply to samplers defined at same level or below them. If none, it applies to the parent sampler only.
Let us understand the second point in detail now.

Assertion Location 1
In the case above, the Assertion only applies to Sampler A.

Assertion Location 2

In this case, the assertion applies to Sampler A and Sampler B because Assertion and Controller are in same level.

Assertion Location 3

In this case, the assertion applies to Sampler A and Sampler B and Sampler C as Assertion is in same level as Controller A and Controller B.

There are multiple assertions available in JMeter. We will go through few assertions which we are using frequently in JMeter test plan.

1. Response Assertion

Response assertion is used to check whether the response contain, match or equal a specified pattern (expected result).

 

In the above picture, Apply to section is used to define the samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources or samples generated by the Transaction Controller and it has 4 different options. 

  1. Main sample - assertion only applies to the main sample
  2. Sub-samples - assertion only applies to the sub-samples
  3. Main sample and sub-samples - assertion applies to both.
  4. JMeter Variable - assertion is to be applied to the contents of the named variable
Response Field to Test:
This option is used to instructs JMeter which field of the Response to test.

Text response – This is for the response which can be displayed in a browser. You can check particular text is present in the response or not.

Document (text) – This is for anything supported by Apache Tika (it assumes apache-tika.jar placed in /lib folder of a JMeter installation). For example: PDF, Office, Audio, Video formats. This can be memory consuming for high loads. I would suggest not to use this unless no other option.

URL Sampled – This assertion is used against a requested URL to ensure that it matches expectations. It’s handy for testing. For example: you may want to check that the redirect URL doesn’t contain an “error” somewhere in the path.

Response Code – This validate the response code which is expected. For 4xx and 5xx response codes, make sure that you have checked the “Ignore Status” box. If not, 4xx and 5xx will give an error even though you specified 4xx/5xx as expected result.

Response Message – This is used to verify that the response message appears as expected.

Response Headers – This is used against Response Headers to see if a specific HTTP Header is present or absent.

Ignore Status – JMeter out-of-the-box considers all 4xx and 5xx responses as failures. If your test case is “negative” and, for example, a “404” error is expected, you’ll need to check this box to suppress JMeter’s built-in status code check and substitute it with your own status code assertion.

This is sample test result.


Let us validate all the options now. In the following example, I am going to hit the URL "https://reqres.in/api/users?page=2" which gives the below response.

{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name":"Lawson","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"},{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg"},{"id":9,"email":"tobias.funke@reqres.in","first_name":"Tobias","last_name":"Funke","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"},{"id":10,"email":"byron.fields@reqres.in","first_name":"Byron","last_name":"Fields","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"},{"id":11,"email":"george.edwards@reqres.in","first_name":"George","last_name":"Edwards","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"},{"id":12,"email":"rachel.howell@reqres.in","first_name":"Rachel","last_name":"Howell","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"}],"ad":{"company":"StatusCode Weekly","url":"http://statuscode.org/","text":"A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things."}}
Example 1: Now I am going to add the response assertion to validate the response. I am expecting "total_pages" content should be part of response. Or else, I assume the response is invalid.
So the option I choose: Field to Test is "Text Response" and Pattern matching rule is "contains" and Pattern to Test is "total_pages"


Response for the sampler is shown below. The expected text "total_pages" is available in the response. Hence, the assertion is passed.


Example 2: I am expecting "200" as a response code. Or else, I assume the response is invalid.
So the option I choose: Field to Test is "Response Code" and Pattern matching rule is "equals" and Pattern to Test is "200"
Response for the sampler is shown below. The expected response code "200" is returned from the server. Hence, the assertion is passed.
Example 3: I am expecting "OK" as a response message. Or else, I assume the response is invalid.
So the option I choose: Field to Test is "Response message" and Pattern matching rule is "matches" and Pattern to Test is "OK"
Response for the sampler is shown below. The expected response message "OK" is returned from the server. Hence, the assertion is passed.
Example 4: I am expecting "Set-Cookie" as part of response headers. Or else, I assume the response is invalid.
So the option I choose: Field to Test is "Response headers" and Pattern matching rule is "substring" and Pattern to Test is "Set-Cookie"
Response for the sampler is shown below. The expected response header "Set-Cookie" is returned from the server. Hence, the assertion is passed.
Example 5: I am expecting "404" as a response code. Or else, I assume the response is invalid.
So the option I choose: Field to Test is "Response Code" and Pattern matching rule is "equals" and Pattern to Test is "404"
Response for the sampler is shown below. The expected response code "404" is returned from the server. However, the assertion is failed (Refer the icon against SINGLE USER NOT FOUND sampler in the below image). The reason for this failure is that JMeter consider 4xx or 5xx as error by default and it wont consider it as passed even if you specifically instructed.
To avoid the above problem,we need to check "ignore status" as well in Field to Test. Refer below 
Response for the sampler is shown below. The expected response code "404" is returned from the server. Hence, the assertion is passed
Hopefully you understood the response assertion in JMeter now.

2 comments:

Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'. Again, we are using the sample file named &#...