Saturday 3 August 2019

API Testing using JMeter

REST (Representational State Transfer) has become the most widely used model for web service implementation. It is lightweight, not strongly typed and unlike SOAP isn't bound to only XML format. So, it has become a hot topic for QAs as well, knowing how to test REST APIs adds some weight to your resume. In my previous posts, I have mentioned about Manual Testing of a REST APIs and REST API Automation using Java. In this post we will see how to do functional or load testing of REST APIs using JMeter.
For this tutorial I am using a dummy REST API available at jsonplaceholder.typicode.com. I will be using the HTTP Post method of the API to request the "post comments" functionality of this dummy API.

API URL - http://jsonplaceholder.typicode.com/posts

API Request Body
{
    title: 'foo',
    body: 'bar',
    userId: 1
}

On successful request, the API would return the below response (as can be seen in View Result Tree Section)-
{
  title: 'foo',
  body: 'bar',
  userId: 1
  id: 101, //or any random id
}

Steps for Scripting a Rest API in JMeter

  • First of all we need to add an HTTP Request Sampler inside a Thread group (Test Plan -> Thread Group -> Sampler->HTTP Request)
    Rest API Load Testing in JMeter
  • Next, we need to enter the Server Name or IP of the API and its port(if required) in the web server section.
    The HTTP Request Section will be required to be filled as-
    • Server Name or IP - Name of the server (e.g. jsonplaceholder.typicode.com)
    • Implementation - Select HttpClient4 or Java
    • Protocol[http] - Depending on type of protocol, select HTTP or HTTPS (e.g. the dummy API is build over http protocol so we'll be using http here)
    • Method - Depending on the type of method, the API was build over, select Get, Put, Post, Delete etc (in our demo we will be working on Post method)
    • Path - Application path after the API_URL (e.g. for API_URL http://jsonplaceholder.typicode.com/posts' the Path will be 'posts'
    • Parameters/Post Body - Add Request of the API body here (request body of the API in "Post body" section (refer to screenshot below for detail, or we can also give requests parameters on by one in Parameters section)
      Rest API Load Testing in JMeter Artoftesting.com
  • User also need to add HTTP Header Manager as child to the HTTP Request sampler(for the current example this is not required, even will not work) with the content-type header
    Click on Add button on HTTP Header Manager and add "Content-Type" under Name and "application/json" under Value.
    Rest API JMeter
  • [Optional]Next, we can add different assertions to the test plan in order to mark the test case as pass or fail based on the response fetched. E.g. the above dummy API returns the "title:foo" in its response. So, in order to test the API we can add a "Response Assertion" to the "HTTP Request"(HTTP Request->ADD->Assertion->Response Assertion) and add the pattern "title: 'foo'" in the Pattern to Test section of Response Assertion. Refer to screenshot for reference.
    Rest API Functional Testing
  • At last we can add different listeners like 'View Result Tree' to conclude. On running the JMeter script (Ctrl+r), we will see the response in for the API in the response data section of "View Result Tree".
    Rest API JMeter View Result Tree
In this way we can do the functional testing of the Rest APIs and increase the "Number of Thread" and "Loop Count" in the Thread Group for load testing it.

No comments:

Post a Comment