How to Automate API Testing Using Postman
Tags: postman, api, testing, automation
Published: 2022-03-01 11:23:45


![postman-api-testing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33plsdkm7uo52ndz430b.png)

Hey, How are you? Must be tired of testing the Same API again and again, I guess that’s why you are here🙊️.

Well, No worries. I have good news for you, Let’s automate it. 😁️

![automation-gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t49l3nwnzjfwwi2557yn.gif)

APIs are the heart of any backend application. Whether it is service-based or monolithic architecture, it requires robust and efficient APIs to support business requirements.

In most applications, APIs contain business logic and database operations so a loophole in a single API can crash the application. As requirements keep adding, the application grows and it’s hard to test each and every developed API.

Most of the developers (Including me 😛️) becomes lazy, when writing the unit-test cases. But when requirements grows, these test-cases becomes boon to fast and secure development. And we just have to write it once so DO IT.

Automating API testing to ensure fast development and code quality would be a great help for an API developer.
Postman is a collaborative API development and testing tool which takes care of both manual and automated API testing and is widely used by many developers.

In this blog, I will take some use-cases to demonstrate how you can setup tests for your APIs through the visual interface as well as CLI.

## **Getting Started 🚀**

For a better understanding, let’s take an example of a user record create API, and we have to login into the system to use the API. So the scenario is that every time you need to test user-create API, you will first call the login API, store the token to process further. Let’s understand our use-case

**Scenario**: Store JWT token from the Login API’s result into the environment and then call user-create API, with that token.

**Pre-request Script**: A section to set up test data for APIs. Here we will set up user-name and password for our login API and user information for creat-API.

**Test**: A section where we can test/store API responses. Here we will check the status code and store the token in the postman environment for our Login API. Also, we will add some validation test cases for user-created API.


## Automating testing with Postman GUI.

**Steps:**

- First, we will create a Login API request in postman.

![step1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jl5vgk3qlyp21mhfx4p.png)

- Now, we will add a pre-request Script for setting the username and password.

![step2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ufy81m4h1e6zxn8j3i2.png)

As shown. a variable is set in the pre-request script. now we will use that in the body of our request.

![step3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hec7lpn1u5gwi9g1b5p.png)

Pre-request script syntax is similar to the test cases that we write programmatically (for eg. jest) So if you know it, bonus 😉

For more, [Pre-request scripts in postman.](https://learning.postman.com/docs/writing-scripts/pre-request-scripts/)

- Next, We will add a test to save a token to the environment on a 200-OK response.

![stpe4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njgdvgsma4lqqkic3tk4.png)

Token will be saved in the currently selected environment.

- Now we will set that environment variable in Authentication as Bearer token to call user-create API.

- For that, we can use a pre-request script(useful when running collection through CLI). or we can directly use the Authorization tab of the postman. We can set it from the headers tab as well. (Postman is so cool 🤩)

- We will use the Authorization tab

This way we can set different validations tests in Test to check the API response.

**Example**

1. Unique username of User

![example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/romek6tgagucupfexu49.png)

Now we will save all the requests in a postman collection and we will run it.

![run collection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4spctzmpduvp21qqtz05.png)

You can set the sequence of a requests while running the collection.

- Test Results

![Test Results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h9kp3hwrarf8ojqudyp.png)

## Automating testing Using CLI

If you are a CLI lover, Postman got you covered there too. 😎️

Postman CLI package is a [npm](https://www.npmjs.com/) package, and requires node to be installed in your device to run. 

Check the Detailed Information about [Newman](https://www.npmjs.com/package/newman).

Let’s dive right into it.

**Steps:**

1. for running tests from CLI, you need to have a postman collection. So I suggest you to setup requests, tests, environment and export the collection and environment file from postman to process further.

2. Install newman in your device using npm.
`npm install newman`

3. Run the collection
`newman run ${collection_file_path}`

4. It will generate a report in your Console that will look like this.

![report1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8leouzvgbvsmxf1ad0q.png)

![report2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwo7qlxaeh78nmh15exk.png)

The report contains API responses and test results.

There’s one catch of using this package is that, you can not change the sequence of the API requests.

I hope now you know, How to unit test all your APIs in automate mod️e and won't crash your server.😉️


![testing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjzokhbub5nthmnfi0qk.gif)

**References**:
[Postman](https://learning.postman.com/docs/getting-started/introduction/)

**About me**:
I am a Javascript Developer at [DhiWise](https://www.dhiwise.com/). It’s pro-code devtool that generates production ready code for 6 technologies. Check it out if you are tech-geek 😋️

You can find me on [LinkedIn](https://www.linkedin.com/in/saloni-saraiya/) and we can talk about cool devtools like the one I am developing.

Keep coding👩‍💻️, keep testing. Bye. 👋️