Assertion in JMeter is used to validate response of the request that you have sent to the server.
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.
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.
- Main sample - assertion only applies to the main sample
- Sub-samples - assertion only applies to the sub-samples
- Main sample and sub-samples - assertion applies to both.
- JMeter Variable - assertion is to be applied to the contents of the named variable
{"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"
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.
ReplyDeleteGreat blog with good information.
JMeter Training in Chennai
JMeter Online Training
JMeter Training in Bangalore
Useful blog, keep sharing with us.
ReplyDeleteScope of JMeter
Why JMeter