9 Steps To Automate Your API Testing With Postman Effectively


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.

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). The aim of API testing is 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, that 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 developer 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 already installed.

Beginning the tutorial

1. First create your test project folder. For example: example-api-testing

2. 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”: “[email protected]:appsierra/example-api-testing.git”

    }

 }

3. 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.

4. 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.

5. 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: “[email protected]”,

phoneNumber: “9998887777”,

role: “editor”

})

.expect(200)

.end(function (err, res) {

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

expect(res.body.email).to.equal(“[email protected]”);

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();

});

});

});

6. 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

7. To place your test runner command create entrypoint.sh 

#!/bin/bash 

case $1 in

“run”)

npm start

;;

*)

echo “usage: $0 [run]”

exit 1

;;

esac

8. For Jenkins CI create a dockerfile

FROM node:7.8-slim

# app workdir

WORKDIR /app

# copy app dependencies

COPY package.json ./

# install dependecies

RUN npm install -g mocha mocha-jenkins-reporter

RUN npm –allow-root install

# build app source code

COPY . ./

# runtime configs

ENTRYPOINT [“./entrypoint.sh”]

9. Now create you 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.

10. 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.