Beginner-friendly REST API Testing Guide - Using Postman

Now you already have an idea about what is the RESTful API, and how it looks like.

So, what's next?

How we can start to test it? And what we should test?

You can start by learning how to use Postman to do some manual testing first.

What is Postman


Postman is one of the most popular tools used in API testing. It is a powerful tool that allows you to create tests easily, store information in different environments, and store data for use in other tests. 

For a beginner who has no experience, the user interface of the Postman matches the basic concepts we introduced here. Hopefully using Postman is quite straight forward for you.


Download the Postman



Let's download a Postman and install it first, you can use it in chrome or as an individual application. 
After installing the extension or app and launch postman, it will ask you to sign up for free. It allows you to share your collections, sync data across different devices, and back up your data.  


You can choose to skip this step for now. 

Have another look at API


Now let's have a look at the API again. 


In here, we have 5 types of request: GET, POST, PUT, PATCH, DELETE

We will use Postman to create some examples for GET, POST, and PUT, which will be the most commonly used method. 

We will use the following APIs, hopefully, you already go through these APIs and have a bit understanding around it:
  • GET SINGLE USER 
  • POST CREATE
  • PUT UPDATE
Create A New Request

Now let's try to create a new API request.


We will need to create a folder to store all the tests, let's use "Reqres.in" as the folder name, and using "GET SINGLE USER" for Request name for our first API test.



This post will show you how to use Postman to cover the basic concepts we introduced in the last tutorial:
  • HTTP
  • JSON
  • URIs
Let's have a look at a GET request:


In the grey area, the first dropdown box is the HTTP Request method, and by default, it's a "GET" request. 

Next to it is where to put the URL of an API. 


For this GET request, we don't need anything in the request Body. 


After putting the URL "https://reqres.in/api/users/2" in the URL input box, try to click the "Send" button, you will get the response from Postman.



This is how to get an API response in your first request. 

Now, where's the testing part? Do we need to validate anything? 


Yes. You can easily validate a few things: 

  • HTTP Response Status Code
  • Response
  • JSON Body
HTTP response status codes can indicate whether a specific HTTP request has successfully completed. You can find details in another REST API Tutorial

And you can also validate if the response is in a specific format or not, such as JSON.


You can also validate if the JSON body includes specific fields or values.


Let's have a look at Status Code first.


Click the "Tests" tab, and write the first test here to verify that you get a 200 status code for response: 



1
2
3
4
 // example using pm.response.to.have
 pm.test("response is ok", function () {
     pm.response.to.have.status(200);
 });

Click the "Send" button again, as you can see, other than the response, there is a test result display in the "Test Results" tab:



If you update the status code in Tests to "201", and click the "Send" button again, the test will fail. 


If you're interested to learn more, you can always find detailed documentation about how to create tests from Postman in here.

Create a POST request


Now we write a simple test to cover the GET request, let's have a look at the POST request.


Have a look at the https://reqres.in/


If you click the GET - SINGLE USER API, it will provide a URI.


If you click the POST - CREATE API, it will provide a JSON body as well as a URI.


So when we create the request in Postman, we need to include the JSON body as well.




Now if you click the "Send" button, you will get a response with Status Code 201 Created, which means a new resource has been created. And here, we created a new user.






Now we want to write a test to validate both Status Code return as 201, and the new user has the correct name or job as we put into the request JSON body.

// example using pm.response.to.have
pm.test("response is ok", function () {
    // assert that the status code is 201
    pm.response.to.have.status(201);
});


// example using pm.expect()
pm.test("response include job of new created user as Tester", function () {
    //put response Body into jsonData
    var jsonData = pm.response.json();
    // assert that the response has job field as Tester
    pm.expect(jsonData.job).to.eql("Tester");
});


Try to run it again with the tests we just added.


Wrap up

Now let's do a quick wrap up: 
  • You learned why you want to use Postman.
  • You learned how to create a GET and POST request and get the response.
  • You learned how to create a JSON body in the request.
  • You learned how to write a simple test to validate the status code and JSON body.
You might ask, how about PUT request? 

Well, I want to leave the PUT request to you and do some practice by yourself. 

Further Reading and Practice


You will never learn everything from reading one blog, or one book, or one piece of code. We won't able to cover everything here. The purpose of this tutorial is to get you started. 


We will provide you some extra reading, or practice, so you can carry on with your learning after this.


We strongly recommend that you start to build the habit of reading documentations. Postman has its own documentation and will cover all the topics you need to use Postman in your work environment, or for self-study. 

You can find the Postman documentation here.

If you want to do some practice about create API requests, write tests using Postman, you can go through all the API list in https://reqres.in/ and try it out.

Postman Collection File


Postman allows you to export the test suite and share it with others. 


Here are a few API tests I created for GET, POST, and PUT.  


You can download and import to Postman and have a look. 


What's Next

In the next tutorial, we will start to talk about some basic programming concepts, and prepare you to write some automation using rest-assured. 


Comments