Automate the API testing with Postman.

Atul KumarAtul Kumar
5 min read

Automated API testing with Postman is a valuable practice that helps developers and testers ensure the quality and functionality of their APIs. Postman is a user-friendly and powerful tool designed to simplify API testing and streamline the testing process. This document will provide an overview of how to conduct automated API testing using Postman.

Benefits of Automated API Testing with Postman:

  1. Time Efficiency: Automating API testing with Postman saves time by running tests automatically, freeing up resources to focus on other critical tasks.

  2. Consistency: Automated testing ensures that API tests are performed consistently without manual errors, improving reliability and repeatability.

  3. Faster Feedback: Rapid execution of test scripts allows for quick feedback on API changes, enabling developers to fix issues promptly.

  4. Scalability: Postman supports testing multiple APIs simultaneously, making it suitable for projects of various sizes.

  5. Integration with CI/CD: Automated API testing can be integrated into Continuous Integration and Continuous Deployment (CI/CD) pipelines, ensuring thorough testing at every code change.

If you're familiar with Postman basics, like sending requests and using collections, now let's dive into creating tests in Postman.

Writing test cases in Postman

Before writing test cases in Postman, ensure you have the following:

  1. Postman is installed on your system.

  2. A basic understanding of APIs and HTTP methods.

  3. Familiarity with JSON (JavaScript Object Notation) for response validation.

Where to write test cases.

In Postman, you can write and organize your test cases using the "Tests" tab within the Postman request editor. Here's how you can write test cases in Postman:

  • Create a Request: First, create a request by selecting the request type (GET, POST, PUT, DELETE, etc.) and entering the request URL along with any necessary headers, parameters, and request body.

  • Navigate to the Tests Tab: After setting up the request details, click on the "Tests" tab located below the request editor. This is where you'll write your test scripts.

  • Write Test Scripts: In the "Tests" tab, you can write JavaScript code to define your test scripts. These scripts will be executed after sending the request, and you can use them to validate the response received from the server.

Here's a simple example of a test script that checks if the response status code is 201:

pm.test("Response status code is 201", function () {
    pm.expect(pm.response.code).to.equal(201);
});

You can add more test scripts to validate various aspects of the response, such as response headers, response body, specific data values, etc.

  • Run the Request: After writing your test scripts, you can click the "Send" button to send the request to the server. Postman will execute your test scripts and display the results in the "Tests Results" section below the request editor.

  • View Test Results: The test results will show you whether your test scripts passed or failed. If a test fails, Postman will provide information about the failure, which can help you identify the issues in your API.

  • Organize Test Scripts: You can organize your test scripts by creating multiple requests within a Postman collection. Each request can have its own set of test scripts. Collections allow you to group related requests and tests together, making it easier to manage and run your test suite.

Remember that the examples provided here are basic illustrations. You can write more complex test scripts using various assertions and conditions based on your API testing needs.

Additionally, Postman offers documentation and tutorials to help you learn more about writing test scripts and performing API testing effectively.

Refer this article for common test scripts in postman 👉 Click Me

Integrate Postman automated testing with CI/CD

Now We’ll learn about How to integrate Postman automated testing with CI/CD of GitHub using Newman in the Nest JS Project.

Integrating Postman automated testing with CI/CD using Newman involves setting up a process that allows you to run Postman collections and tests automatically as part of your Continuous Integration and Continuous Deployment (CI/CD) pipeline. Newman is a command-line tool provided by Postman that allows you to run Postman collections from the command line, making it suitable for automation in CI/CD environments.

I’m assuming that you have already created a collection with test cases.

Here's a step-by-step guide on how to integrate Postman automated testing with CI/CD using Newman.

Generate Postman Collection API URL.

For generating Postman collection API Url, Follow the given steps:

  1. Click on the “View More Actions“ button of the collection.

  2. Click on the “Share“ Option

  1. Choose the “Via API” option.

  1. Copy the Postman API URL using copy button.

Your Collection API Generated Successfully.

Configure Your CI/CD Pipeline:

  • You'll need to set up a job or stage that will run your Postman tests using Newman.

  • In the pipeline configuration, you'll need to specify the commands to install Newman, run your tests, and possibly report the results.

To configure your CI/CD pipeline, Follow the given steps:

  1. Create a .github folder in the root of the project.

  1. Create the workflows folder inside the .github folder.

  2. Create yourFileName.yml file inside the workflows folder.

  1. Now, write a shell script to run your Postman tests as part of the build or test phase.

     name: Test Nest.js Application
    
     on:
       push:
         branches:
           - create-company // Replace with your branch name
    
     jobs:
       run-and-test:
         runs-on: ubuntu-latest
         steps:
           - uses: actions/checkout@v2
           - name: Install Node.js
             uses: actions/setup-node@v2
             with:
               node-version: '18'
           - name: Install Nest.js CLI
             run: npm install -g @nestjs/cli
           - name: Install Dependencies
             run: npm install
           - name: Run Nest.js Application on Localhost
             run: npm run start:dev & sleep 10
           - name: Run API Tests with Newman
             run: |
               npm install -g newman
               newman run "Paste Generated Collection API Url"
    

    For more such articles visit: https://tech.pococare.com

9
Subscribe to my newsletter

Read articles from Atul Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Atul Kumar
Atul Kumar

I am a full-stack web developer.