1
Automation testingQa

How To Add Response Assertions On JMeter

2 Mins read

This blog post will teach you how to add response assertions on JMeter. If you have not installed JMeter, see How to install JMeter and run it. For a basic introduction on JMeter, read here on How To Run JMeter Tests Step By Step With Examples.

What is an assertion?

An assertion in testing is a condition that should be true for the test to succeed. JMeter has assertions to verify that the text value matches, that person exists in the system, or to check if an endpoint is alive. These are just a few of the assertions JMeter offers you.

It’s not often that when working with response assertions in JMeter, content does not match the expectations for when the assertion (a keyword) was triggered. This can lead to failed assertions or unexpected results in amplitude and latency monitoring reports. Fortunately, it is possible to fix this.

How To Add Response Assertions On JMeter

Assertion scenario

Our API returns a POST response as shown below from this URL. The following response is received.

Response

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

Our test assertion is to assert that

a) userId = 1

b) id = 1

Test Case #Test Case DescriptionPre-requisites/ Special ConditionsTest StepsExpected Results/ Acceptance CriteriaTest ResultsCommentsTest durationTest Priority
1When the API is called, a userId = 1, should be returned as part of the response– Call the POST get endpoint /posts/1
– Assert that userId = 1
UserId should be equal to 1PASS2 minsHigh
2When the API is called, a id = 1, should be returned as part of the response– Call the POST get endpoint /posts/1
– Assert that id = 1
id should be equal to 1PASS1minHigh
3
Test cases

Jmeter Assertions step-by-step

Create a new Thread group and name it Assertions Thread Group, leave all other details at default for the sake of this tutorial.

Right click on the Response Assertion > Add > Sampler > HTTP Request

Right click on the GET HTTP Request > Add > Assertions > Response Assertion

Step II – Configuring the assertion

First, Let’s add a listener to view results

Right click on the Thread > Add > Listener > View Results in Table

We will now add the assertions for userId = 1 and id = 1.

JMeter can read the response given and check whether the test response given contains the assertions we want.

We do that by configuring the assertions as below.

On GET Response Assertions

  1. Select Text Response
  2. Select Contains
  3. Click Add at he bottom and Input the params to test on each row. ie “userId”: 1 and “id”: 1

Running the assertions

Great, now that everyithing is set up, let’s run the assertions and view them in the results tree. Click the green start button and switch to the View Results in Table Tab

As you can see, the Status is green – indicating a success – meaning that all our assertions have returned true.
To test for a failure, change the assertion parameters under GET Response Assertions and see a failure as below.

Failing the test on purpose

We will give wrong assertions – so that we expect a failure.

Notice that the “userId” is now 10 and not 1. Let’s run the tests.

Failed results

Great!
As expected, notice the second run of the test now fails – since the assertion was incorrect.

Conclusion

That was just a tip of the iceberg on how assertions work on JMeter.
Assertions are very useful for determining if results are within expected ranges or not. Assertions can also be used alongside other elements in batch scripts and loops where they act as guards to stop batches when certain conditions aren’t met. Sometimes it’s more convenient than trying to figure out why something isn’t working by stepping through code line-by-line in debug mode during a test run, which is time consuming and inefficient.

Read more on Why You Need To Have Test Documentation In Software Testing.

Don’t miss amazing tips!

1
Related posts
Automation testingQaRuby

Selenium Ruby Example And Deployment To The Cloud Grid

13 Mins read
Table of contents0.1 What is Selenium?0.2 How does Selenium WebDriver in Ruby work?0.3 How to use Selenium with Ruby?0.4 How to run…
General testingInterestingQa

Available Career Growth Paths In Software Testing

3 Mins read
Table of contents0.1 What Are The Available Career Growth Paths In Software Testing?0.2 Junior Level Possible careers:0.3 Mid Level Possible careers:0.4 Senior…
General testingQa

If You Want To Find Bugs - Do More Exploratory Testing

3 Mins read
Table of contents0.1 Exploratory testing basics0.2 Tips to get started with exploratory testing0.3 1) Set the purpose of your exploratory testing0.4 2)…

Leave a Reply

Your email address will not be published. Required fields are marked *

57 + = 65