Assertions in JMeter: Response Assertion with Examples

⚡ Smart Summary

Assertions in JMeter verify that a server under test returns the expected response, letting performance testers validate response content, status codes, response time, byte size, and document format during load testing for accuracy.

  • Response Assertion: Matches patterns in fields of the server response.
  • ⏱️ Duration Assertion: Flags any response slower than the set limit.
  • 📦 Size Assertion: Checks the response size in bytes.
  • 🧾 XML and HTML Assertion: Validate XML or HTML response syntax.
  • 🤖 AI Assistance: AI suggests assertions and flags result anomalies.

Assertions in JMeter

What is an Assertion?

Assertions help verify that your server under test returns the expected results.

Types of Assertions

Following are some commonly used Assertion in JMeter:

  • Response Assertion
  • Duration Assertion
  • Size Assertion
  • XML Assertion
  • HTML Assertion
  • Steps to use Response Assertion

Response Assertion

Response Assertion

The response assertion lets you add pattern strings to be compared against various fields of the server response.

For example, you send a user request to http://www.google.com and get the server response. Response Assertion can verify whether the response contains an expected pattern string (e.g. “OK”).

Duration Assertion

The Duration Assertion tests that each server response is received within a given amount of time. Any response that takes longer than the specified milliseconds is marked as failed.

For example, a user request is sent to www.google.com by JMeter and get a response within expected time 5 ms then Test Case pass, else, test case failed.

Duration Assertion

Size Assertion

The Size Assertion tests that each server response contains the expected number of bytes. You can specify the size as equal to, greater than, less than, or not equal to a given number of bytes.

JMeter sends a user request to www.google.com and gets a response packet smaller than the expected 5000 bytes, so the test case passes; otherwise it fails.

XML Assertion

The XML Assertion tests that the response data consists of a formally correct XML document.

XML Assertion

HTML Assertion

The HTML Assertion allows the user to check the HTML syntax of the response data. It means the response data must be met the HTML syntax.

HTML Assertion

Steps to use Response Assertion

We will continue on the script we developed in the earlier tutorial.

In this test, we are using Response Assertion to compare the response packet from www.google.com matches your expected string.

Here is the roadmap for this test:

Steps to Use Response Assertion

The response assertion control panel lets you add pattern strings to be compared against various fields of the response.

Step 1) Add Response Assertion

Right-Click Thread Group -> Add -> Assertions -> Response Assertion

Add Response Assertion

Response Assertion Pane displays as below figure:

Add Response Assertion

Step 2) Add Pattern to test

When you send a request to Google server, it may return some response code as below:

  • 404: Server error
  • 200: Server OK
  • 302: Web server redirects to other pages. This usually happens when you access google.com from the outside USA. Google re-directs to country-specific website. As shown below, google.com redirects to google.co.in for Indian Users.

Add Pattern to test

Assume that you want to verify that the web server google.com responses code contains pattern 302,

On Response Field To Test, choose Response Code,

On Response Assertion Panel, click Add -> a new blank entry display -> enter 302 in Pattern to Test.

Add Pattern to Test

Step 3) Add Assertion Results

Right click Thread Group, Add -> Listener -> Assertion Results

Add Assertion Results

Add Assertion Results

Step 4) Run your test

Click on Thread Group -> Assertion Result

When you ready to run a test, click the Run button on the menu bar, or short key Ctrl+R.

The test result will display on the Assertion Results pane. If Google server response code contains the pattern 302, the test case is passed. You will see the message displayed as follows:

Run The Test

Now back to the Response Assertion Panel, you change the Pattern to test to from 302 to 500.

Run The Test

Because Google server response code doesn’t contain this pattern, you will see the test case Failed as following:

Run The Test

Troubleshooting

If you face the issue while running the above scenarios … do the following:

  1. Check whether you are connecting to the internet via a proxy. If yes, remove the proxy.
  2. Open a new instance of JMeter
  3. Open the AssertionTestPlan.jmx in JMeter
  4. Click on Thread Group -> Assertion Result
  5. Run the Test

FAQs

An assertion validates a response as pass or fail; a listener only displays the results.

Yes. The JSON Assertion checks values in a JSON response, as the XML Assertion validates XML.

Each assertion parses the response and adds CPU cost, so limit assertions to critical samplers.

Yes. AI assistants read a sample response and generate the matching JMeter assertion automatically.

AI flags anomalies in response codes or timing that fixed assertions might miss.

Summarize this post with: