Blog

A Step by Step Guide to Test REST API With JMeter

Written by Charan Sai Dasagrandhi | Dec 10, 2020 9:53:53 AM

REST and SOAP APIs are the most widely used web services tool to install plugins, libraries and software. To successfully build web services using these architectures, it is important to test the services thoroughly. To ensure successful deployment of REST and SOAP APIs, a strong testing mechanism is key. Here we discuss how to perform REST API testing using JMeter. 

How to Test REST APIs Using JMeter

JMeter is a performance testing tool used to generate loads and it's also used for API testing. Follow these steps to test a REST API using JMeter. 

Details of the API we are going to use:

{"coord":{"lon":78.47,"lat":17.38},"weather":[{"id":301,"main":"Drizzle","description":"drizzle","icon":"09d"}],"base":"stations","main":{"temp":297.96,"feels_like":299.11,"temp_min":297.15,"temp_max":298.71,"pressure":1004,"humidity":94},"visibility":3000,"wind":{"speed":6.48,"deg":261},"clouds":{"all":75},"dt":1594808996,"sys":{"type":1,"id":9214,"country":"IN","sunrise":1594772388,"sunset":1594819438},"timezone":19800,"id":1269843,"name":"Hyderabad","cod":200}

  • Launch JMeter
  • Create a Thread Group inside the Test Plan and keep the number of users and the loop count as 1.
  • Add a Sampler HTTP Request; Inside the HTTP Request provide the Server name and the Path on which we are going to perform the API testing. Example – api.openweathermap.org à (This is just for reference, don't apply a load on a public website. We recommend creating and testing your own website.)
  • Provide the server name as “api.openweathermap.org” in the field provided
  • Provide the path “/data/2.5/weather” in the field provided
  • In the parameters section provide the parameters
  • The first parameter; under the name section enter “q”, under the value section enter the “city name” (Example – “Hyderabad”),
  • Click on the ADD button at the bottom of the screen to add another parameter
  • Second parameter; under the name section enter APPID, under the value section enter the user id
  • Add a listener to display the output, select to display in the form of a tree; select “View result tree” from the dropdown menu
  • Save the ext .jmx in the bin folder (that must be the default just in case it’s not then save the extension as .jmx),
  • After saving the file, run the script and result will be displayed
  • Under the Text section the name “HTTP Request” will be displayed and a “Sampler result” tab will appear
  • Click on “HTTP Request”, the details of the result will be displayed under Sampler result with the Thread name, Sample start date and time, load time, connect time and Response code
  • Click on “HTTP Request” there will be 2 tabs added “Response data" and "Request", Under the response data we will have the same data that we have in our API,
  • If the response code is 200, it means that test was ran successfully
  • Go back to the “Thread Group” and increase the users to 10 and run the test we will see the HTTP Request will be displayed 10 times under the Text section and all of them will be showing the exact status of the API
  • If the icon before the username is green that means the API can take the load of many users. When the icon changes to red that means the test has failed with the increased number of users. 
  • When the test is completed successfully, add a Response assertion in the Thread group
  • In Response assertions, add the response code (200). Once the response code is entered, go to the tree, then run the test again
  • Now if we change the response value to “201”, clear the results, and run the test
  • We see that the user status will be displayed in red. This means the test failed. This failure indicates that the response code in our API and the response code we have provided is not the same
  • So we have to make sure that the response code/value must match

In the above example, we tested a REST API, in the same way, we can test a SOAP API.

Conclusion

The process of testing APIs is easy and seamless when we run the tests through JMeter. JMeter is a powerful tool for Rest API Testing. We can test any API to see how it behaves with a different number of loads. 

About Author

Vinod Kumar works as a QA Lead at V-Soft Digital. He has 13 years of experience with more than 8 years of experience in various performance and other testing projects in the Quality domain of the IT industry. He has worked on various platforms and has developed innovative solutions to optimize performance and products. He is a certified professional in Lean Six Sigma Black Belt (LSSBB) and Scrum Master (CSM). In addition to these, he is certified in Modern Project Management - Quality Risk, Procurement and Project Closure, BA from Udemy, RPA Business Analyst from UI Path and Six Sigma in Healthcare.