BreadcrumbHomeResourcesBlog What Are JMeter Response Assertions? May 28, 2020 What Are JMeter Response Assertions? Open Source AutomationBy Santiago PerazaJMeter assertions are an important feature. And they keep improving! Read this blog to learn the basics.Table of ContentsWhat JMeter Assertions DoJMeter Response Assertions OverviewExamples of JMeter AssertionsTable of Contents1 - What JMeter Assertions Do2 - JMeter Response Assertions Overview3 - Examples of JMeter AssertionsBack to topWhat JMeter Assertions DoJMeter 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 topJMeter Response Assertions OverviewIn JMeter 3.1 and EarlierIn JMeter 3.1, users were able to create assertions based on the combination of:Apply to - where to apply the assertionResponse Field to Test - to which field the assertion should be appliedPattern 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 aboveIf 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 topExamples of JMeter Assertions1. Apply Assertions to Request HeadersThe Response Assertion has a 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 Request2. Configure the Sampler:Name: Blazedemo homepage (for your convenience)Method: GET (selected by default)Server: blazedemo.comPath: /Port: 80Protocol: HTTP3. Add the following request, which find flights from “San Diego” to “New York”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.5. Configure the Response Assertion:Apply to: Main sample onlyField to Test: Request HeadersPattern Matching Rules: ContainsPatterns to Test: Host: blazedemo.com6. After running the test, you can validate the test passed, because the Request Header contains Host: blazedemo.comIn 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 RuleIn 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 useResponse 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.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 LayoutThe 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.1JMeter 3.2This 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 NOWRelated Resources: How to Use JMeter Assertions in Three Easy StepsAdvanced API Assertions with BlazeMeter API Testing and MonitoringHow to Save Response Data in JMeterBack to top
Santiago Peraza Performance Engineer Santiago Peraza is a Performance Engineer. He is a ISTQB certified software tester. Santiago's main areas of expertise are JMeter scripting and executing cloud and on-premise performance tests.