Automating API Testing with Postman (original) (raw)

Last Updated : 18 May, 2026

Automating API testing using Postman allows testers to validate APIs quickly, run repeatable tests, and ensure consistent results. It helps reduce manual effort and improves overall software quality.

Postman Environment Setup

Postman Environment Setup helps manage URLs, tokens, and variables efficiently, making API requests reusable and easier to maintain.

Step 1. Create Environment

Step 2. Add Variables

Define key-value pairs

Step 3. Use Variables in Requests

Replace values using {{variable_name}}

**Example: {{base_url}}/users

4. Select Environment

Choose the environment from the dropdown before sending requests.

**Example:

Instead of writing full URL every time:

https://api.example.com/users

**Use:

{{base_url}}/users

Postman API Testing Automation Workflow

To automate API testing with Postman, we can use JSONPlaceholder - a free online mock REST API used for testing and prototyping. It provides ready-to-use endpoints, allowing testers to simulate real API requests without setting up a backend.

The base URL for all API requests is: **https://jsonplaceholder.typicode.com. Before starting, ensure that Postman is installed on your system.

Step 1: Open Postman application

Launch the Postman application on your system.

Step 2: Create a New Collection

Step 3: Create Requests in the Collection

**Request 1: GET Users

Screenshot-2023-10-18-at-10242-(1)

**Request: POST Create a Post

{

"title": "Post from Postman",

"body": "This is a test post created using Postman",

"userId": 1

}

Click "Save."

Screenshot-2023-10-18-at-11912

Step 4: Write Test Scripts

Now, let's add test scripts to verify the responses. For the "Get Users" request

JavaScript `

// Check if the status code is 200 (OK) pm.test("Status code is 200", function () { pm.response.to.have.status(200); });

// Check if the response has at least one user pm.test("Response has users", function () { // Parse the JSON response const jsonData = pm.response.json();

// Check if the response is an array and has at least one element
pm.expect(jsonData).to.be.an('array').and.to.have.lengthOf.at.least(1);

});

`

Screenshot-2023-10-18-at-13903-(1)

**For "Create a Post" Request:

JavaScript `

Create a new post pm.test('Create a new post', async () => { const response = await pm.request('Create a Post');

// Check the status code pm.expect(response.code).to.equal(201);

// Check the response body pm.expect(response.json()).to.have.property('title'); });

`

Screenshot-2023-10-18-at-13648

Step 5: Run Collection using Collection Runner

The **Collection Runner in Postman is used to execute an entire collection of API requests in sequence. It allows testers to run multiple API tests together, automate execution, and validate responses in one go.

Screenshot-2023-10-18-at-14342-(1)

Screenshot-2023-10-18-at-14930

Install Newman CLI

npm install -g newman

**Note: This will install Newman, which is the command-line Collection Runner for Postman used to execute and automate API tests.

Postman CLI

Once Newman is installed, run it from any directory on your machine. To run tests automatically using Newman, use the following command:

Running Tests Manually:

newman run

Here, the collection file is named JSONPlaceholder API Tests.postman_collection.json, so the following command is used:

newman run "JSONPlaceholder API Tests.postman_collection.json"

Run tests manually

**Scheduling Tests to Run Automatically:

1. Open the Postman app.

Screenshot-2023-10-20-at-10243

2. Enter a name for the monitor and select the collection you want to monitor.

Screenshot-2023-10-20-at-11012

3. Configure a collection-based monitor.

Screenshot-2023-10-20-at-10363

4. Run the tests.

Screenshot-2023-10-20-at-10392-(1)

Best Practices for Automating API Tests in Postman

The following are best practices for Postman API test automation:

Benefits of Automated API Testing

Some of the benefits of automated API testing are:

Limitations of Postman

Postman is a powerful API testing tool, but it also has certain limitations that should be considered while using it for automation and large-scale testing.