JMeter – Response Assertion

In JMeter, Response Assertion is used to validate the presence of a particular string (pattern) or some fields like response code, response message etc. The pattern string could be a number, word, statement etc. which could be present either in the server response or in the JMeter request. 

Example:

I have created a JMeter script of an e-commerce site where I can place an order to buy some products. Once the order is submitted, the application generates an order number which confirms that the order has been submitted successfully. To validate the submission of an order during the test I can use Assertion which will check the order number generation statement and confirm whether the order has been submitted successfully or not. 

How to add ‘Response Assertion’?

Follow the below steps:

  1. Select the ‘Sampler’ element whose response needs to be validated.
  2. Right-click on the element
  3. Hover the cursor on ‘Add’
  4. Hover the cursor on ‘Assertions’
  5. Click ‘Response Assertions’
JMeter - Response Assertion
Figure 01: Response Assertion

What are the input fields of ‘Response Assertion’?

‘Response Assertion’ has the following input fields:

  1. Name: To provide the name of the assertion.
  2. Comments: To provide arbitrary comments (if any)
  3. Apply to: To define the search scope.
    1. Main sample and sub-samples: In case, the request is redirected then use this search scope, so that the given pattern string can be searched in the response of both the main and re-directed requests and then validated.
    2. Main sample only: When the request is not re-directed and the given pattern string value is present only in the response of the main request then use this search scope.
    3. Sub-samples only: When the request is re-directed and the given pattern string value is available in the response of re-directed request only then you can use this search scope.
    4. JMeter Variable Name to use: If given pattern string value supposes to be available in any JMeter variable then you need to select this option and provide the JMeter Variable name in the text field.
  4. Field to Test: This option is used to refine the type of pattern.
    1. Text Response: This option instructs JMeter to search the given pattern string in the body of the response. The header of the response does not include in the search scope.
    2. Response Code: JMeter checks and validates the response code only like 200, 302 etc. 
    3. Response Message: JMeter checks and validates the response message only like OK, Error etc.
    4. Document (text): This option allows JMeter to search the pattern string in the document returned by the server.
    5. Response Headers: The given string is searched in the header part of the response only including cookie data.
    6. Request Headers: This option instructs JMeter to search in the header part of the request. It is helpful when the request is redirected and the pattern string value is present in the header of sub-request.
    7. URL Sampled: The search scope will be URL only when you select this option.
    8. Request Data: This option is used to validate the string available in the request body which is sent to the server by JMeter. It does not include the request header.
    9. Ignore Status: Passing/Failing of assertion having two parts. The first part instructs JMeter to check the successful response code (i.e. 200) and then in the second part, the given pattern string is searched and validated. The overall success of the sample is determined by combining the result of both the conditions i.e. response code and the assertion. When the Ignore Status checkbox is selected, the Response status is forced to be successful even it falls under the unsuccessful response category (like 4XX or 5XX) and continues to validate the Assertion. The “Ignore status” checkbox can be used to set the status (response code) successfully before performing further checks. Note that this will clear any previous assertion failures, so make sure that this is only set on the first assertion.
  5. Pattern Matching Rule: This option is used to set the rule to match the pattern string.
    1. Contains: If regular expression token is given in the “pattern to test” field and the captured statement contains the text which you want to match then this option need to be selected. Example: The expected pattern is “Your order number is (.*?).” The sampler will be passed if it finds the statement (say) “Your order number is perfOrder987650.” which is a confirmation message that order is submitted and an order number is generated; irrespective of checking the order number (which is a dynamic value). 
    2. Matches: In this case, the regular expression statement should be fully matched with the pattern. Example: Response code should be anything 2XX. Hence the pattern would be 2\d\d. 
    3. Equals: This option instructs JMeter to match the exact text with the case-sensitive feature.
      Note: ‘perfmatrix’ and ‘PerfMatrix’ both the string treated differentially.
    4. Substring: JMeter checks and validates the substring from the captured string.
    5. Not: This option instructs JMeter that particular string pattern should not be in the response (or search). It is an invert assertion check. Example: You can select “Response Code” as a field to check, “Not” as a pattern matching rule and write “500” in patterns to test. Then JMeter will pass the sampler until status code 500 is received.
    6. Or: In case of multiple patterns, you want to pass the sampler if any one of the patterns matched then this option is check marked.
  6. Patterns to Test: Here, you can write a list of patterns to be tested. Each pattern is tested separately. If a pattern fails, then further patterns are not checked (if “Or” is not marked). There is no difference between setting up one Assertion with multiple patterns and setting up multiple Assertions with one pattern each (assuming the other options are the same).
  7. Custom failure message: A custom message can be written in this field which is displayed in case of the assertion failure. 
JMeter - Response Assertion
Figure 02
Assertion Result
Figure 03

Some Key Rules related to Assertion:

  1. Do not add so many assertion elements, it may impact the throughput of the request during the test.
  2. Assertion patterns (especially text messages) should be unique on the page to validate responses properly.
  3. You can define Global assertion under the test plan which will be applicable to all the samplers. The global assertion is mainly used to validate the response code.   

You may be interested: