Written By :Appsierra

Thu Sep 14 2023

5 min read

9 Steps To Automate Your API Testing With Postman Effectively

Home >> Blogs >> 9 Steps To Automate Your API Testing With Postman Effectively
blog

API Testing with Postman

Postman provides a comprehensive tool for API testing that makes setting up automated tests simple. The tests and requests you’ve generated can be aggregated into a single automated test series. From the Postman app, Postman monitoring, or from the command line with Newman, Postman’s command-line tool, run and control your test workflow.

API Testing with Postman
API Testing with Postman

Also Read: Getting Started With Postman

Rest API Testing online

REST API Testing is an open-source web automation testing methodology used to test web applications using REST APIs. … Representational State Transition stands for Rest. It is an architectural style used in the creation of web services, API testing services, and a method for interaction. API TESTING is a type of software test which validates APIs (Application Programming Interfaces). API testing aims to check the software interfaces’ functionality, usability, performance, and security. REST Assured is a Java API testing module that provides efficient, manageable tests for RESTful APIs in a domain-specific language (DSL) for writing.

This article will try to solve one or all of these problems for you:

1. Do you face issues in API testing manually?

2. Are you a QA/QE and using Postman/Pew/Insomnia for testing your Rest API endpoints testing manually?

3. Do you want a separate test from the app?  

Pre-Requisites

Node.js – Node.js is an open-source cross-platform. It’s a Javascript runtime built-in Chrome V8 JavaScript engine. It uses an event-driven single-thread non-blocking I/O, which makes it lightweight and efficient. Node.js has the largest ecosystem of packages with an open-source library and npm in the world.

Mocha – It is a JavaScript framework that makes nonsynchronous testing easy.

Chai– It is an assertion library that is used in Node and additionally used mocha as a supplement. Chai lets you choose the assertion interface that you like including “assert”, “except” and “accept”.

Supertest– Supertest is an npm module and an additional extension of Superagent. A lightweight AJAX request library. It provides abstractions for testing node.js API endpoint response.

Docker – It is an open-source and powerful PAAS for developers and sysadmin to build, ship, and run the applications. Docker can be used in any platform whether it be Cloud, VM, or in laptops.

Jenkins CI – Jenkins is an open-source automation server for developers that helps in automating the non-human part of the software development process, as well as common things like continuous integration.

In this tutorial article, we assume you already have –

  1. Docker and Node.Js installed in your machine.
  2. API of App that you want to test.
  3. Jenkin CI is already installed.

9 Steps To Automate API Testing with Postman

Following are the 9 crucial steps to automate your API Testing with postman effectively: 

1. Create your test project folder. For example example-API-Testing 

Create a package.json file to add all dependencies

{    “name”: “example-api-testing”,

    “version”: “1.0.0”,

    “description”: “Example API Testing”,

    “author”: “AppSierra”,

    “dependencies”: {

        “body-parser”: “^1.17.1”,

        “chai”: “^3.5.0”,

        “mocha”: “^3.2.0”,

        “mocha-jenkins-reporter”: “^0.3.7”,

        “supertest”: “^3.0.0”

    },

    “scripts”: {

        “prestart”: “npm install;”,

        “start”: “JUNIT_REPORT_PATH=test-result/result.xml JUNIT_REPORT_STACK=1 mocha –timeout 25000 –colors –reporter mocha-jenkins-reporter || true”

    },

    “repository”: {

        “type”: “git”,

        “url”: “git@github.com:appsierra/example-api-testing.git”

    }

 }

2. Install All Dependencies by These Commands

cd your_test_project_folder

npm install -g mocha

npm install

All the node modules should be added to the testing module folder and all the dependencies should be added successfully.

3. Create a Test Folder in Your Project Testing Module/File

cd your_test_project_folder

mkdir test

The directory must include a test for Mocha to run the test files.

4. Create Your API Test File 

You can name anything to your test files. For example, you can name just user_test.js within the test directory. Also, set your API URL to a global variable.

ar should = require(‘chai’).should(),

expect = require(‘chai’).expect,

supertest = require(‘supertest’),

api = supertest(‘http://localhost:3000’);

describe(‘User’, function () {

var location1;

var location2;

var location3;

var locations = [location1, location2, location3];

before(function (done) {

api.post(‘/locations’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

addressStreet: “111 Main St”,

addressCity: “Portland”,

addressState: “OR”,

addressZip: “97209”,

userId: 1

})

.expect(‘Content-Type’, /json/)

.expect(200)

.end(function (err, res) {

location1 = res.body;

});

api.post(‘/locations’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

addressStreet: “222 Main St”,

addressCity: “Portland”,

addressState: “OR”,

addressZip: “97209”,

userId: 2

})

.expect(‘Content-Type’, /json/)

.expect(200)

.end(function (err, res) {

location2 = res.body;

});

api.post(‘/locations’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

addressStreet: “333 Main St”,

addressCity: “Portland”,

addressState: “OR”,

addressZip: “97209”,

userId: 3

})

.expect(‘Content-Type’, /json/)

.expect(200)

.end(function (err, res) {

location3 = res.body;

done();

});

});

it(‘should return a 200 response’, function (done) {

api.get(‘/users/1’)

.set(‘Accept’, ‘application/json’)

.expect(200, done);

});

it(‘should be an object with keys and values’, function (done) {

api.get(‘/users/1’)

.set(‘Accept’, ‘application/json’)

.expect(200)

.end(function (err, res) {

expect(res.body).to.have.property(“name”);

expect(res.body.name).to.not.equal(null);

expect(res.body).to.have.property(“email”);

expect(res.body.email).to.not.equal(null);

expect(res.body).to.have.property(“phoneNumber”);

expect(res.body.phoneNumber).to.not.equal(null);

expect(res.body).to.have.property(“role”);

expect(res.body.role).to.not.equal(null);

done();

});

});

it(‘should have a 10 digit phone number’, function (done) {

api.get(‘/users/1’)

.set(‘Accept’, ‘application/json’)

.expect(200)

.end(function (err, res) {

expect(res.body.phoneNumber.length).to.equal(10);

done();

});

});

it(‘should have the role of admin’, function (done) {

api.get(‘/users/1’)

.set(‘Accept’, ‘application/json’)

.expect(200)

.end(function (err, res) {

expect(res.body.role).to.equal(“admin”);

done();

});

});

it(‘should be updated with a new name’, function (done) {

api.put(‘/users/1’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

name: “Kevin”,

email: “kevin@example.com”,

phoneNumber: “9998887777”,

role: “editor”

})

.expect(200)

.end(function (err, res) {

expect(res.body.name).to.equal(“Kevin”);

expect(res.body.email).to.equal(“kevin@example.com”);

expect(res.body.phoneNumber).to.equal(“9998887777”);

expect(res.body.role).to.equal(“editor”);

done();

});

});

it(‘should access their own locations’, function (done) {

api.get(‘/users/1/location’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

userId: 1

})

.expect(200)

.end(function (err, res) {

expect(res.body.userId).to.equal(1);

expect(res.body.addressCity).to.equal(“Portland”);

done();

});

});

it(‘should not be able to access other users locations’, function (done) {

api.get(‘/users/2/location’)

.set(‘Accept’, ‘application/x-www-form-urlencoded’)

.send({

userId: 1

})

.expect(401)

.end(function (err, res) {

if (err) return done(err);

expect(res.error.text).to.equal(“Unauthorized”);

done();

});

});

});

5. Now Let’s Run This Command for API Testing

cd your_test_project_folder

npm startorcd your_test_project_folder

JUNIT_REPORT_PATH=test-result/result.xml JUNIT_REPORT_STACK=1 mocha –timeout 25000 –colors –reporter mocha-jenkins-reporter

6. To Place Your Test Runner Command Create Entrypoint.sh 

#!/bin/bash 

case $1 in

“run”)

npm start

;;

*)

echo “usage: $0 [run]”

exit 1

;;

esac

7. For Jenkins CI, Create a Dockerfile

FROM node:7.8-slim

# app workdir

WORKDIR /app

# copy app dependencies

COPY package.json ./

# install dependencies

RUN npm install -g mocha mocha-jenkins-reporter

RUN npm –allow-root install

# build app source code

COPY . ./

# runtime configs

ENTRYPOINT [“./entrypoint.sh”]

8. Now Create Your Jenkins Job Items

Ignore the warning on the first test result, as it will disappear after you run the job.

Then click the SAVE button to create the job.

9. Run the Job and See the Test Result

Congratulations, you have successfully automated your API Testing.

Happy Testing!

If you have any other questions regarding testing, please address them in the comments section below. We will be happy to help you.

Also Read: Guide to API Development

Our Popular Articles

appsierra

Services

Data & Analytics

Cloud

Engineering and R&D

Quality Engineering

Application Development

Enterprise IT Security

DevOps

AI & ML Engineering

Infrastructure Service Management

Industries

Hitech and Manufacturing

Banking, Insurance & Capital Markets

Retail and Consumer Goods

Healthcare, Pharma & Life Sciences

Hospitality, Leisure & Travel

Oil, Gas & Mining Resources

Power, Utilities & Renewables

Technology, Media & Telecommunications

Transportation & Logistics

Technology CoE

SAP

Microsoft

Oracle

Salesforce

ServiceNow

HR Technology

5G and Edge

ADAS, Connected Car

IoT/Embedded System

Copyright© 2023. All Rights Reserved