Skip to content

Month: November 2018

API Testing – A Google Postman Tutorial

So you have an interest in using Postman as your API Testing tool?

Awesome! But before we begin, please be sure to read my blog on Postman found here. 

Let’s get started! First, navigate to my tutorials github repository and download the project.

Here is a very basic Postman tutorial for testing and training purposes. This repository contains the following files: The Postman Collection(PostmanTutorial.postman_collection), The Data File( postman.data.json), and this ReadMe file.

Requirements:

Instructions:

  1. Download/Clone this repository.
  2. In a new Terminal window, navigate to your newly downloaded/cloned postman-tutorial project.

Running the Collection:

  1. In a new Terminal Window, Navigate to the folder containing the PostmanTutorial.postman_collection and the postman.data.json files. (cd into the project directory).
  2. Run $newman run PostmanTutorial.postman_collection --insecure -d postman.data.json

Editing the Data Tested:

  1. Open the postman.data.json file in your preffered IDE, notepad or terminal window.
  2. Make edits, save, and ReRun the collection.

Editing Tests:

  1. Open Postman
  2. Once Postman is open, On the Top Navigation Bar, click Import
  3. Import the PostmanTutorial.postman_collection file.
  4. Navigate to your newly imported PostmanTutorial collection in Postman.
  5. Click Tests to view the Tests for the collection.
  6. Click Save to save your changes.
  7. Hover over your Postman Collection name and click the three dots ()
  8. Cick Export
  9. Select the Collection v2 option and click Export
  10. Save to the same folder location and overwrite PostmanTutorial.postman_collection.
  11. Run your Collection.

Generating Run Reports:

  1. Install newman html reporter by doing a $npm install -g newman-reporter-html in a terminal window.

Usage:

In order to enable this reporter, specify html in Newman’s -r or --reporters option.

  • Example: $newman run PostmanTutorial.postman_collection --insecure -d postman.data.json -r html

API Testing & Google Postman

Google has always been one of the leaders in the tech industry and although there are many different options when it comes to selecting the best API testing tool, Google’s Postman still is at the top of my list and here is why.

What is an API?

Great question! So, in computer programming, an application programing interface (API) consists of a subset of definitions, communication protocols, and tools for building software.

Basically what this means is that a set of clearly defined methods of communication amongst various components. This could be a web-based system, Operating system, database system, computer hardware, or even a software library.

 

What makes a “Good” API?

A good API makes development easier. Think of it as the rock, or the foundation of a project. You need a solid base foundation and building blocks the piece together the end result, right? Same goes for API and programming.

 

What is Google Postman?

Google Postman “makes API Development simple, developers use Developers use Postman to build modern software for the API-first world.”

Not only can Postman be used for developing an API, it can also be used for Testing an API.

Google Postman Features

Postman’s Tools Support Every Stage of the API Lifecycle.

Postman Cost – FREE!

The best thing about Postman is that it is completely free! I have used Postman for a few years now and have never had a problem with the Free version and have used Postman for testing multiple API’s.

 

How can I use Google Postman for API Testing?

Google postman can be used for API testing in a variety of different ways. For example, you can have a simple status check on a website’s Url that runs on a monitor schedule and runs at the top of every hour, and can even notify you when a website is unreachable with the ability to check HTTP Status Codes.

You can also test a REST API’s response body to verify that the API is providing the expected request response.

 

Example Postman Tests:

// Status Code Tests:

if(tests[“Status code is OK/200 and receiving a response as expected”] = responseCode.code === 200) {

   // Response Assertions/Status Checks

   pm.test(“Status code is OK”, function () {

   pm.response.to.have.status(“OK”);});

 

   pm.test(“Response time is less than 800ms”, function () {

   pm.expect(pm.response.responseTime).to.be.below(800);});

 

   pm.sendRequest(“https://postman-echo.com/get”, function (err, response) {

       console.log(response.json());});

 

   pm.test(“Content-Type is present”, function () {

       pm.response.to.have.header(“Content-Type”);});

 

// Error Checks:

   pm.test(“Response should be okay to process”, function () {

   pm.response.to.not.be.error;

   pm.response.to.have.jsonBody(“”);

   pm.response.to.not.have.jsonBody(“error”); });

 

// Response Body Content Tests:

   pm.test(“Response contains A String”, function () {

       pm.expect(pm.response.text()).to.include(“ExpectedResponseStringHere”);});

}

 

The above Example is of possible Test Scripts that can be written to test API’s using Postman. The Status Code Tests are testing that the response was Status Code 200 (Ok) and that the Response was received in less than 800ms/0.8 seconds.

The Error checks are checking that the response should be okay to process and is a .json as expected.

 

The final test is testing that a Specific String from the Response Body is included in the API’s response.

View Postman Test Scripts for more information.

Postman Review

Overall I am incredibly happy with Postman and everything that it can do in terms of being able to Test an API. Postman has a fantastic UI and is easy to learn and use for beginners. With the ability to automate scheduled runs and checks on API’s it makes testing easy and powerful. Postman’s free option is all that is needed when testing an API. If you are new to API Testing, and wanting to get your feet wet, I strongly recommend you give Postman a try.

Links:

Download Google Postman

A How-To test using Postman Tutorial

Getting Started with Google Postman