Skip to content

How To Automate Rest Api Using Postman

API Testing Blog

Automating REST API Testing with Postman

Postman is a powerful tool for interacting with APIs, and it goes beyond just sending requests and viewing responses. It allows you to automate your API testing process, making it more efficient and reliable.

1. Creating and Organizing API Tests with Postman Collections

Postman Collections offer a structured way to organize your API tests. They allow you to group related requests and test scenarios, making it easy to manage and execute your tests.

Steps:

  1. Create a New Collection: Navigate to the “Collections” tab in Postman and click the “Create Collection” button. Give your collection a descriptive name.
  2. Add Requests: Within your collection, click the “Add Request” button and create requests for each API endpoint you want to test. Provide the necessary information like HTTP method (GET, POST, PUT, DELETE), URL, headers, and body parameters.
  3. Add Tests: Each request can have associated tests, which are code snippets written in Javascript that verify specific aspects of the response.
  4. Organize Tests: Use folders within the collection to group related tests for better organization and maintainability.

Example Code:

// Example test to verify a successful GET request
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Example test to check if the response body contains specific data
pm.test("Response body contains a specific field", function () {
pm.expect(pm.response.json().name).to.be.equal("John Doe");
});

2. Setting Up Environment Variables for Flexible Testing

Environment variables are crucial for dynamic testing, allowing you to easily switch between different environments (development, testing, production) without having to manually change request parameters.

Steps:

  1. Create an Environment: In the “Environments” tab, create a new environment by clicking the “Add Environment” button.
  2. Add Variables: Define variables within your environment and set their corresponding values. You can use these variables within your requests and tests.
  3. Select Environment: Before executing your tests, select the appropriate environment from the drop-down list.

Example Code:

// In the request URL, you can use environment variables:
{{baseUrl}}/users/{{userId}}
// In your tests, you can access environment variables:
pm.test("User ID is correct", function () {
pm.expect(pm.response.json().id).to.be.equal(pm.environment.get("userId"));
});

3. Writing Assertions for Validation using Postman’s Test Runner

Postman provides a powerful test runner that allows you to write assertions to validate the behavior of your API. You can test various aspects, including status codes, response bodies, headers, and timing.

Steps:

  1. Add Tests to Requests: After creating a request, navigate to the “Tests” tab.
  2. Write Assertions: Utilize the Javascript-based test runner to write assertions using pm.test() and various methods like pm.response.to.have.status(), pm.expect(), and pm.response.json().
  3. Run Tests: Execute the request and the associated tests. Postman will display the results of each test, clearly indicating whether they pass or fail.

Example Code:

pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response body contains an ID", function () {
pm.expect(pm.response.json().id).to.be.a('number');
});

4. Using Pre-request Scripts to Modify Requests

Pre-request scripts let you modify requests before they are sent, enabling you to dynamically generate data or modify request parameters.

Steps:

  1. Add a Pre-request Script: Within a request, navigate to the “Pre-request Script” tab.
  2. Write Javascript Code: Use Javascript to modify the request object. For example, modify headers, add query parameters, or dynamically generate request bodies.
  3. Execute the Request: Perform the request, and the pre-request script will be executed before the request is sent.

Example Code:

// Generate a random email for the request body
pm.environment.set("email", "test" + Math.random() + "@example.com");
// Set the email address for a POST request
pm.request.body.email = pm.environment.get("email");

5. Leveraging Postman Collections Runner for Automated Execution

The Postman Collections Runner automates the execution of your collections, allowing you to run multiple tests in sequence with configurable settings.

Steps:

  1. Open Collections Runner: Navigate to the “Runner” tab.
  2. Choose Collection: Select the collection you want to run.
  3. Configure Settings: Adjust the environment, iteration count (running multiple times), delay between requests, and other settings.
  4. Run Collection: Click “Run” to start the automated execution of your tests.
  5. View Results: Postman displays the results of each request and test in a visually appealing way.

6. Integrating Postman with Your CI/CD Pipeline

You can seamlessly integrate Postman into your CI/CD pipeline, allowing it to become an integral part of your automated testing process.

Steps:

  1. Configure Postman API Key: Obtain the Postman API key from your Postman account.
  2. Set Up CI/CD Environment: Choose your preferred CI/CD platform (e.g., Jenkins, GitHub Actions, CircleCI).
  3. Install Postman CLI: Install the Postman CLI using npm to access the Postman API.
  4. Create a Script: Write a script to execute your Postman collection using the Postman CLI tools.
  5. Integrate into Pipeline: Include the script within your CI/CD pipeline and configure it to run regularly.

Example Code:

Terminal window
# Run a Postman collection named "MyAPI"
newman run 'path/to/MyAPI.json' -e 'path/to/environment.json'

Final Thoughts

Automating your REST API testing with Postman is a game-changer for your software development process. By leveraging Postman’s collection functionality, environment variables, assertions, pre-request scripts, and automated testing capabilities, you can ensure consistent quality, improve efficiency, and accelerate your development work.

API Testing Blog