API Functionality Testing Using JMeter

Apache JMeter is a popular open-source software used for performance testing.

In this blog, we will shed light on how to use JMeter for REST API automation for functionality testing.

We’ve used sample APIs from https://reqres.in/site, which lists multiple real APIs that can be used for testing. We selected user management APIs, which can register user, login user, list all users, create users, update users, and delete users.

Listed of APIs used for this testscript:

/api/users?page=2

/api/users/2

/api/register

/api/login

Using JMeter, we considered the following sample REST end points:           

  • Use HTTP POST to add new users.
  • Use HTTP GET to retrieve a list all the users.
  • Use HTTP PUT to update newly added users.
  • Use HTTP DELETE to delete the users added.

Step 1 : Create new test plan —>Right click—> Add —>Threads—> Thread group

  1. Set the number of threads (users) to 10 – We will have 10 users executing the test plan.
  2. Set the Ramp-up period (in seconds) to 10 – The Ramp-up period tells JMeter about the length of delay before dealing with the next user.
  3. Set the Loop count to 10 – number of times to execute the test.

Note: Logic controllers determine the order in which user requests are executed. Simple controllers are just containers for user requests.

Step 2Thread group —>Right click —>Add —>Logic controller —> Simple controller

Step 3Simple controller—> Right click —>Add—>Sampler — >Http  Sampler Request

POST Method

POST request is used to create a new record in the data source.

To add new users.

Step 1: Create a POST method request for registering a new user with two parameters, email and password, and rename it as “registration success”.

  • Enter the protocol as “https” and IP address as “reqres.in”.
  • Select the method as “POST”.
  • Enter the API path. Similarly, we can login successfully and create users with the “POST” method.

  • Name: Post an Item
  • Method: POST
  • Body Data: JSON Item Object

In response assertion add patterns to be tested

Step 2 Create a text file and save the parameters in that file.Jmeter uses CSV data dynamically for storing multiple data objects, update data values, and more.

Step 3Thread group → Add Http header manager

Add the content-Type and token

Step 4: Save and run the registration success testcase .

Step 5 : Threadgroup–>Add–>Listener–> View Results Tree

GET method

GET method is used to retrieve information in the data source.

To retrieve a list of all users.

Step 1 : Simple controller —> Right click —> Add —> Another Sampler —> Http  Sampler Request

  • Add another HTTP sampler request.
  • Enter the protocol as “https” and IP address as “reqres.in”.
  • Select the method as “GET”.
  • Enter the API path of the list user.

  • Path: /users/${userId}
  • Method: GET

Step 2: Listusers—>Right click —>Add –>Assertions–>response assertions

Step 3Listusers→Add—>Listeners

  • Save and run the “listusers” testcase.
  • See the response data it list all the users .

PUT method is used to update an existing record in the data source .

To update the users added.

Step 1: Simple controller —> Right click —> Add —> Another Sampler —> Http  Request Sampler

  • Add Another HTTP Request Sampler  and name as “updateusers”.
  • Set the method to ‘PUT’.
  • Enter the protocol as “https”and ip address as “reqres.in”.
  • Enter the API path.
  • Add the user’s details as body data.

Step 2 : The values passed directly into the post data.

Step 3Update users–>Add—>Assertion–>Response assertion

Step 4 : Save and run the “updateusers” testcase. See the response data  for the updated users .

DELETE Method

DELETE request is used to delete an existing record in the data source.

To delete users from the list.

Step 1: Simple controller —> Right click —> Add —> Another sampler —> Http  request sampler:

  • Add another HTTP Request Sampler  and name as “deleteusers”.
  • Set the method to ‘DELETE’.
  • Enter the protocol as “https” and IP address as “reqres.in”.
  • Enter the API path.
  • Add the user’s details as body data.

Step 2Delete users–>Add—>Assertion–>Response assertion

Step 3 Save and run the “Deleteusers” testcase.See the response data  for the deleted users .

In the end, we can add different listeners like “View Results Tree” to conclude. To add, right click and select Listeners → View Results Tree.

Here, you can see the request and response data for each sample request for each testcase.

As an example, we have scripted for one negative testcase below:

Register unsuccessful

Step 1: Simple controller —> Right click —> Add —> Another sampler —> Http  sampler request:

  • Add another HTTP request sampler  and name as “Register unsuccess”.
  • Set the method to ‘POST’.
  • Enter the protocol as “https” and IP address as “reqres.in”.
  • Enter the API path.
  • Add the user’s details in the POST body data.

Step 2 : Register unsuccessful->Add—>Assertion–>Response assertion

Step 3Save and run the “register unsuccess” testcase.See the response data  for the register unsuccess  users .

response code from view results
Response data from view results

Summary Report for the above script .

Assertion Results

There are various reports available in JMeter. We can use them based on the project necessity.

Here, we have showed how to do functionality testing in JMeter using REST API AUTOMATION with screenshots.