API Testing

API Testing using JMeter

The Representational State Transfer (REST) uses the HTTP request method, and the most popular are GET, POST, PUT and DELETE. Responses to these requests return status codes indicating success or failure as well as any applicable headers, and JSON representing the affected fields (or nothing) in the message body.

Apache JMeter is a popular open-source software used for performance testing. we can use Jmeter to test API 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 2: Thread group —>Right click —>Add —>Logic controller —> Simple controller

Step 3 : Simple 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 3: Thread 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 3 : Listusers→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 3 : Update 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 2 : Delete 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 3 : Save 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.

Categories: API Testing, JMeter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s