May 28, 2020

What Are JMeter Response Assertions?

Open Source Automation

JMeter assertions are an important feature. And they keep improving! Read this blog to learn the basics.

Back to top

What JMeter Assertions Do

JMeter assertions allow us to validate whether the actual response contains, matches, or equals the expected response, or whether it doesn’t contain, doesn’t match or doesn’t equal the expected response. Hence, response assertions are a key element of your JMeter test, so you must be glad to hear they have been improved even more.

Back to top

JMeter Response Assertions Overview

In JMeter 3.1 and Earlier

In JMeter 3.1, users were able to create assertions based on the combination of:

  • Apply to - where to apply the assertion
  • Response Field to Test - to which field the assertion should be applied
  • Pattern Matching Rules - whether the expected the response should (Not)Contain, (Not)Match, (Not)Equal or (Not)Substring)
  • Patterns to Test - the expression you are testing according to the rules above

response assertions in jmeter 3.1

If assertions are new to you, please take a look at this post, “How to Use JMeter Assertions in Three Easy Steps”, to learn how to use JMeter Assertions.

Back to top

Examples of JMeter Assertions

1. Apply Assertions to Request Headers

The Response Assertion has a new interface:response assertions in jmeter 3.2, new interface

As you can see, the “Request Headers” testing field has been added and can be asserted against. Developers should found this pretty useful, as this allows them to validate Header values such as Authorization, Cache-Control, Connection, Cookie, Host, Origin or Referer, among others.

By using a debugging proxy tool like Fiddler, you can access the target site and check which Header Values are sent in the Request Header. Then, you can use the new Response Assertion feature to make sure you are sending the same values in your JMeter script.

Let’s look at an example. We created a simple script to simulate a user that navigates to blazedemo.com (a flight reservation site for testing purposes), selects departure and destination cities, and then finds flights for the selected cities.

To build the script from scratch, follow these steps:

1. Right click on Thread Group → Add → Sampler → HTTP Request

jmeter, add http request

2. Configure the Sampler:

  • Name: Blazedemo homepage (for your convenience)
  • Method: GET (selected by default)
  • Server: blazedemo.com
  • Path: /
  • Port: 80
  • Protocol: HTTP

jmeter, configure http request

3. Add the following request, which find flights from “San Diego” to “New York”

jmeter, http request, find flights

4. With this setup, the request POST http://blazedemo.com/reserve.php should have blazedemo.com in the Host value of it Request Header.  So, let’s add a simple validation using the new Response Assertion - Apply to - Request Headers feature.

To do that, right click on the request and then navigate to Add → Assertions → Response Assertion.

jmeter, add response assertion
5. Configure the Response Assertion:

  • Apply to: Main sample only
  • Field to Test: Request Headers
  • Pattern Matching Rules: Contains
  • Patterns to Test: Host: blazedemo.com

jmeter response assertion, request headers

6.  After running the test, you can validate the test passed, because the Request Header contains Host: blazedemo.com

jmeter, response assertion, test passed

In the same way we can validate other Request Headers values. This will give us more information about why a request might be failing.

2. Assert Multiple Responses with the “Or” Matching Rule

In previous JMeter versions, if you had to validate a request response that had more than one valid response, you probably had to add more than one Response Assertion element. You also had to add logical controllers that instructed JMeter to use

Response Assertion “A” in some cases and Response Assertion “B” in others.

Now with JMeter 3.2 you can use one single Response Assertion element. You can add as many "Patterns to Test" as you want, and then just select the “Or” Pattern Matching Rule to tell JMeter to mark the request as successful if either of the assertions succeed. This is a great feature and we are glad that Apache JMeter™ introduced this improvement in their last release.

To test this improvement, we will continue working with the previous script. Now, we want to add a validation that will be marked as successful if there are flights available from either Virgin America or United Airlines.

7. To do so, simply add a new Response Assertion element, check the “Or” option and add both Virgin America and United Airlines above the Patterns to Test section.

jmeter response assertion, or option

With this configuration, the test will be marked as passed if there are flights available from either of the two airlines, and with only one assertion.

3. Cell Renderer Has an Improved Layout

The Cell renderer of Patterns to Test has also been improved. The new layout is tidier and the lines are numbered. This is really helpful when you have long text validations.

JMeter 3.1

jmeter 3.1 cell renderer layout

JMeter 3.2

jmeter 3.2 cell renderer layout

This improvement is pretty useful when you need to validate the response structure, i.e: HTML or JSON.

These Response Assertions improvements will make your JMeter scripts more configurable (thanks to the new Apply to Request Headers feature), more powerful (thanks to the new Pattern Matching Rules “Or”) and more readable (thanks to a better cell renderer).

START TESTING NOW


Related Resources: 

Back to top