Automate API Testing with Postman

Search for a command to run...

No comments yet. Be the first to comment.
Testing is an integral part of the software development process. When it comes to web development, testing React components is crucial to ensure that your application functions as expected. Playwright is a powerful tool that can help you automate bro...

Firebase Cloud Messaging (FCM) is a versatile cloud-based messaging platform that empowers app developers to send messages and notifications to users' devices, delivering text, images, links, and more. In this guide, we'll explore the benefits of usi...

In this article, we will look at some usual test scripts that people often use when they write test cases. In Postman, test scripts are written using JavaScript in the "Tests" tab after sending an API request. Each test script is associated with a sp...

What is GHCR? GHCR stands for Github Container registry, The GitHub Container registry allows you to host and manage your Docker container images in your personal or organization account on GitHub. One of the benefits is that permissions can be defin...
Pococare
6 posts
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.
Time Efficiency: Automating API testing with Postman saves time by running tests automatically, freeing up resources to focus on other critical tasks.
Consistency: Automated testing ensures that API tests are performed consistently without manual errors, improving reliability and repeatability.
Faster Feedback: Rapid execution of test scripts allows for quick feedback on API changes, enabling developers to fix issues promptly.
Scalability: Postman supports testing multiple APIs simultaneously, making it suitable for projects of various sizes.
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.
Before writing test cases in Postman, ensure you have the following:
Postman is installed on your system.
A basic understanding of APIs and HTTP methods.
Familiarity with JSON (JavaScript Object Notation) for response validation.
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:
COPY
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
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.
For generating Postman collection API Url, Follow the given steps:
Click on the “View More Actions“ button of the collection.

Click on the “Share“ Option



Your Collection API Generated Successfully.
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:

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

Now, write a shell script to run your Postman tests as part of the build or test phase.
COPY
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"