Assertions in JMeter examples

Assertion helps to confirm that server under test returns the expected result. Assertions allows us to define the pass/fail criteria for application under test. We can configure what kind of assertions is active for every test plan.

Example, if a web response doesn't contain an expected expression, it would be considered as a failure.

Assertions can be added to any controller in a test plan by simply right-click on the controller and add an assertion. Assertions can be applied to a main sample and its sub-samples, or only to sub-samples.

We can add more than one assertion to the test plan, sampler, controller or thread group. Failed assertions will cause all affected samples to fail. i.e. If the main sample is successful and the sub-sampler fails, then the status of the main sample will be set to failed status.

Here is a list of available assertions in JMeter:

  • Response Assertion
  • Check Response Assertion Example here

  • Bean shell Assertion
  • BSF Assertion
  • Compare Assertion
  • JSR223 Assertion
  • Duration Assertion
  • Size Assertion
  • XML Assertion
  • SMIME Assertion
  • MD5Hex Assertion
  • HTML Assertion
  • XPath Assertion
  • XML Schema Assertion

Let us now look into Response Assertion:-

The most commonly used assertion is 'Response assertion' which allows user to add pattern strings comparing against various fields of the response.

Response assertion can be applied to Main sample only, Sub-samples only, Main sample and sub-samples , JMeter Variable.

Apache JMeter Timer

Main sample and sub-samples - assertion applies to both.
Main sample only - assertion only applies to the main sample
Sub-samples only - assertion only applies to the sub-samples
JMeter Variable - assertion is to be applied to the contents of the named variable

Response Field to Test , instructs JMeter which field of the Response to test.

Apache JMeter Timer

Text Response - the response text from the server, i.e. the body, excluding any HTTP headers.
Document (text) - the extract text from various type of documents via Apache Tika (see View Results Tree Document view section).
URL sampled
Response Code - e.g. 200
Response Message - e.g. OK
Response Headers, including Set-Cookie headers (if any)
Ignore Status When the 'Ignore Status' checkbox is selected, the Response status is forced to set successful before evaluating the Assertion.

Example - HTTP Responses with statuses in the 4xx and 5xx ranges are resulted as 'unsuccessful'. The "Ignore status" checkbox can be used to set the status successful before performing further checks.

Pattern Matching Rules, Indicates how the text being tested is checked against the pattern.

Apache JMeter Timer

Contains - true if the text contains the regular expression pattern
Matches - true if the whole text matches the regular expression pattern
Equals- true if the whole text equals the pattern string (case-sensitive)
Substring - true if the text contains the pattern string (case-sensitive)

Equals and Sub-string patterns are plain strings, not regular expressions. NOT may also be selected to invert the result of the check.

Patterns to Test, A list of patterns to be tested. Each pattern is tested separately. If a pattern fails, then further patterns are not checked

Source: Apache JMeter

JMeter Response Assertion Example :

Step 1: Right Click Test Plan > Add > Threads(Users) > Thread Group. Click to know more about Thread Groups and example

Step 2: Thread Group > Add > Config Element > HTTP Request Default. Click to know more about HTTP Requests Samplers and example
Set Server Name as:

Step 3: Thread Group >Add > Sampler > HTTP Request (Change name to Google Home Page)
Set Path as: /

Step 4: HTTP Request (Google Home Page) > Add > Assertions > Response Assertion. Here we are applying Assertion only to this Sampler.

By default, "Response Field To Test" is set to 'Text Response'. Here when we select 'Text Response' Which checks for the response text from the server, i.e. the body, excluding any HTTP headers.

and "Pattern Matching Rules" is set to Sub-string - Which returns true if the text contains the pattern string (case-sensitive).

Now Add pattern to test as shown in the below image.

Apache JMeter Timer

Step 5. To view the assertion results, we need to add an Assertion Listener to the Thread Group

Thread Group > Add > Listener > Assertion Results

Step 6: Thread Group > Add > Listener > View Results Tree

After all the above steps are done, it should look like below :

Apache JMeter Timer

Step 7: Now Save and Run the Test Case

Below is the Assertion Results look like, If the response is failed, it will display a message as 'Response Assertion : Test failed: text expected to contain title 'Boogle'. Here instead of Google, changed it to 'Boogle', As the response data will not have this text which results in Failure.

Apache JMeter Timer

Below is the 'View Results Tree' look like, To confirm manually, try to search for Google in the response data as shown in the image below

Apache JMeter Timer

JMeter Response Assertion Example for Response Code:

In the Same way we can also use Response Code - which checks to ensure the response code is expected. In the above example At step 4, at Section 'Response Field To Test' - select 'Response Code' and add pattern to test with status code '200' as in the screen shot below.

Apache JMeter Timer

Please you try to Run by choosing different response field to test. Feel free to comment below if you face any issue with Assertion.

JMeter Tutorials: 

Add new comment