fbpx

Testing is a crucial part of every web application. It helps you understand the functionality of a specific program to assure it works fine when it finally meets real users. These tests allow developers and companies to identify any bottlenecks. Plus, it saves time and secures the monetary resources to be extra utilized later.  Likewise, API (Application Programming Interface) testing is a type of software testing that determines whether a newly developed application meets the expectations in terms of reliability, performance, operations, and security. However, when focusing on REST API (a type of API), this allows two or more systems to communicate through HTTP, which is quite similar to web browsers and servers, to analyze the response time to each request made by a user.

So if you want to know more about REST API automation testing with the help of a short tutorial, dive in!  But before jumping on the tutorial, it is extremely important to understand what API, REST, REST API, and automation testing is all about. So, let’s cover them briefly one by one.

 

What is an API?

Wondering what an API is? Well, it’s a set of rules which help programs to communicate with each other. The developers create APIs on the servers, which permit clients to talk to them.

 

What is REST?

REST stands for ‘Representational State Transfer’. It is a software architectural style that possesses rules (constraints), which are necessary to be followed by the developers. Nevertheless, one of the most vital constraints is that the web application should be able to deliver the data (information) whenever a command is given.

 

What is a REST API?

All the above information brings us nicely to the REST API. Let’s get this term through some examples.

So you are trying to find an airline that offers inexpensive tickets. You write in the search engine, and you get a whole list of companies. In the same way, when you are trying to search for videos on YouTube, you simply type the keyword or name in the search field and hit enter. The next thing you see is a long list of related videos.

More to this, when you open Google maps and look for a specific place you want to visit, you can also see the nearby amenities, such as restaurants and commute options.  That happens because the companies have exposed their APIs, and REST API works almost in a similar way. You prefer to request some data using HTTP (or in simple words, the Internet), and you receive information or results from the companies you are requesting from.

Despite REST API playing a major role in almost every sphere, the e-commerce industry has been using this technology widely, as it’s helping businesses by handling multiple users commands (requests) and returning distinct data formats.  So in short, from big business groups to normal individuals, it is serving the entire hypermedia that even includes someone who is just trying to upload an image, edit the profile information, or simply download a file.

 

Performing REST API Testing

There are two ways to test the REST API:

  1. Manual testing
  2. Automation testing

However, this tutorial is for automation testing. Also, there are important things to understand to interpret the results including methods and status code.

 

HTTP Methods for REST API Automation Testing

REST API uses five HTTP methods to request a command:

GET: To retrieve the information at a particular URL.

PUT: To update the previous resource or create new information at a particular URL.

PATCH: For partial updates.

POST: It is used to develop a new entity. Moreover, it is also used to send information to sever, such as uploading a file, customer information, etc.

DELETE: To delete all current representations at a specific URL.

 

HTTP Status Codes

Status codes are the response given by a server to a client’s request. They are classified into five categories:

  1. 1xx (100 – 199): The response is informational
  2. 2xx (200 – 299): Assures successful response
  3. 3xx (300 – 399): You are required to take further action to fulfill the request
  4. 4xx (400 – 499): There’s a bad syntax and the request cannot be completed
  5. 5xx (500 – 599): The server entirely fails to complete the request

These codes help interpret the outcomes. So, if the REST API automation test results are between 2xx ranges that means the application’s functions are working ideally.

 

REST API Testing Tutorial with Postman API Testing Tool

The following tutorial is for REST API automation testing using Postman. It’s a famous API testing tool that allows developers to test, share, create, and document APIs. The client is ideal to create and save the complex, as well as, simple HTTP/s requests along with their responses. Postman can be downloaded as a Chrome extension, or you can visit the official website to download the program.

 

Test Scenario: Status Code Weekly

This is a sample REST API automation testing scenario to test all five methods on Status Code Weekly to find the status code of the website.  Let’s begin!

Testing GET Request

Note: After clicking this link (https://reqres.in/api/users?page=2) you would see the JSON code.

But the best thing about Postman is that if you have the API URL, you can simply paste it to get the status code, and the same rule applies to all other methods.

Test Tutorial: After launching Postman, select the GET method and copy the GET request link (https://reqres.in/api/users?page=2) and paste it in the ‘Enter request URL’ field. Once done, click ‘Send’ to see the ‘Status.’

Results: You can see that the status is ‘200 OK.’

 

Status Code Weekly 200

 

Interpretation: ‘OK’ signifies that the GET request is functioning without errors as the status code is exactly 200.

 

Testing POST Request

Step 1: Click on ‘New Tab’ and select ‘POST’ from the drop-down menu.

Postman Post Request Screen

 

Step 2: Copy and paste the POST request URL (https://reqres.in/api/users) in the ‘Enter Request URL’ field and hit the ‘Send’ button.

Results: The status code is ‘200 Created.’

 

Status Code POST Request 201

 

Interpretation: ‘Created’ means that the POST request has succeeded and a new resource has been generated.

 

Testing PUT Request

Step 1: Open ‘New Tab’ and choose ‘PUT’ from the options.

Postman PUT Request Screen

 

Step 2: Copy and paste the PUT request URL (https://reqres.in/api/users/2) in the ‘Enter Request URL’ field, and click ‘Send.’

Results: Status is ‘200 OK.’

Status Code PUT Request 200

 

Interpretation: PUT request is functioning.

 

Testing PATCH Request

 

Step 1: Select ‘PATCH’ in the ‘New Tab.’

Postman PATCH Request Screen

 

Step 2: Paste the PATCH request URL in the field and click ‘Send.’

Results: Status code is ‘200 OK.’

Status Code PATCH Request 200

 

Interpretation: PATCH request is functioning properly.

 

Testing DELETE Request

 

Step 1: Create a ‘New Tab’ and this time select ‘DELETE’ from the menu.

Postman DELETE Request Screen

 

Step 2: Enter the DELETE request URL in the field and click the ‘Send’ button again.

Result: You can see that the Status is ‘204 No Content.’

 

Status Code DELETE Request 204

 

Interpretation: As the link has no content to be deleted, therefore, we get a ‘204’ response. However, if there were any content on the URL, it would surely be deleted with this command as the status code is in the typical range.

 

REST API Testing Tutorial using REST Assured

 

REST Assured is a Java library for testing RESTful APIs. It is widely used to test JSON and XML based web applications. Furthermore, it fully supports all methods including the GET, PUT, POST, PATCH, and DELETE.

Still, you are required to have Java, Maven, TestNG, and IDE (IntelliJ, Eclipse, etc.) already installed in your system to run the test.

Step 1: Install Java

Step 2: Download an IDE (Eclipse)

Step 3: Install Maven and get it linked with Eclipse

Step 4: Create a Maven project first by adding dependencies to ‘pom.xml’. After that, create the test script, make sure to verify, and then run it.

 

Rest Assured Maven Dependency

Maven dependency

 

 

 

 

 

 

TestNG Maven Dependency

Maven dependency TestNG

 

 

 

 

 

Assuming you have installed all the tools, let’s test the same GET Request, you have seen in the Postman, on Eclipse using REST Assured to find out the results. If you want, you can apply the same method to test other requests.

So, follow these steps for GET Request:

Step 1: Open ‘Eclipse.’

Step 2: Create a new ‘Java Package’ and name it.

Step 3: Create a new ‘Java Class’ and name it. For instance ‘Get Data.’

Step 4: Now copy or write the same code mentioned below:

 

1 package getRequest;

2

3 import org.testing.Assert;

4

5 import io.restassured.RestAssured;

6 import io.restassured.response.Response;

7 

8 public class GetData

9 {

10

11 @Test

12 public void testResponsecode()

13 {

14

15 

16 Response resp=RestAssured.get("https://reqres.in/api/users?page=2");

17

18 int code=resp.getStatusCode();

19 

20 System.out.printin(" Status code is "+code);

21

22 Assert.assertEquals(code, 200);

23

24 }

25

26

27 }

28

It should look like this:

 

GetData Java

 

Step 5: Now, right click near the code, hover on ‘Run As’, and click ‘TestNG.’

Run As > TestNG

Result and Interpretation: As you can see, the status code is 200 and it has ‘Passed’ the test.

TestNG Passed

 

Conclusion: REST API Automation Testing Tutorial

REST API allows two or more applications to communicate among themselves, as well as permitting users to command programs to get desired results. There are different ways to test REST APIs, but the most suggested process is through automation testing. And although there are some famous REST API testing tools, Postman and Rest Assured are currently in the spotlight.  In this tutorial, we have tested some requests on both interfaces and shared the results. As expected, both platforms have shown similar outcomes. However, using REST Assured for REST API automation testing is more reliable as compared to Postman. But it demands professional developers.

If you’re using Postman for API functionality, and looking to execute load tests against API to determine behavior under load, the LoadView platform can help.  Learn more about API testing with Postman.  And if you’re curious to know more, don’t hesitate to get in touch with our team.

Start your LoadView trial and get $20 in load testing credits.