TESTEROPS

A pragmatic approach to QA and OPS

Testing REST API’s with Postman and Jenkins – Part 1

When you’re working with platforms that are using RESTful API’s then it becomes really important to test your API’s . Not only does it let’s you know the performance and health of the API’s, but also let’s you know if there are any errors or issues with the API’s , which could become the reason for the downfall of the whole application.

In today’s market, RESTful API’s are being used a lot. Whether developed using Python DJango-REST platform, or using JavaScript based NodeJS, I see an increasing shift towards use of REST API’s.

This leads many to question -when testing with REST API’s what the things that you need to consider and what tests should you include? I’m glad you asked. There are a lot of things to be considered when testing rest API’s.  Here is a full checklist of all the things that you need to consider doing before/during/after your API- and in particular REST API’s testing.

 

WHY POSTMAN

 

I have used a lot of API’s tools – from SmartBear’s SOAP UI, to REST Console, but hands down, POSTMAN Chrome App has been the simplest, easy to use, and easy to debug tool I have seen.

Reasons-

  • Easy to use – It has a really nice GUI interface, which lets you perform the your tests very easily.
  • Grouping of API’s – Postman lets you create Collections of various API calls that you can run at once- which is a really nice feature.
  • In built JavaScript Tests – Postman lets you write in built tests, using a basic JS syntax, which is very handy for asserting the API requests and response.
  • Integration with CI – Postman has a very nice CLI feature, called Newman, which can be used to integrate it with CI systems like Jenkins, for Continuous integration and delivery, which is a huge plus point.
  • Availability of libraries for tests– There are external libraries that can be used for testing API’s response ,apart from the built in tests that Postman Provides.

 

In the subsequent lines, you’ll see how we can combine Postman, Jenkins and Postman-BDD library to come up with a API testing framework , integrated with CI.

 

 PREREQUISITES

 

  • POSTMAN APP

To start with writing tests for our REST API’s, we are going to have to install Postman. Installation of Postman is one of the easiest tasks in the world. Earlier Postman used to come only as a Chrome app, but now, you can get a native client for both Mac and Windows.

Since I’m on a Mac, I’m gonna go ahead, and install the Chrome app from app store, which is going to add Postman as a Chrome app on the machine.

In case you want  to read about how to install the native client, you can head over to the Postman docs here, where the whole installation process is discussed in detail.

 

  • POSTMAN INTERCEPTOR

Postman Interceptor is the extension available for Chrome that can capture and manipulate HTTP requests passing between Postman and web servers. This is what the Postman docs says about the interceptor

Postman Interceptor is an extension available for Chrome that can capture and manipulate HTTP requests passing between Postman and web servers. It can capture network requests directly from Chrome and save them to Postman’s history. This means you can debug your web apps APIs in real time! There is no need to install or configure a proxy. There are no code changes required either. You can filter requests according to the URL based on a regular expression. If you have a web app for which you don’t have a collection built already, or you just want to debug the APIs that your app is using, this can save a lot of time. Built on the Chrome platform, the feature works effortlessly across Windows, Linux, Mac and Chrome OS. It can also capture and manipulate cookies or set certain HTTP headers that are blocked on the Chrome platform by default.

You can install interceptor also as a Chrome app, by simply clicking on this app store link here.

Once you have Postman and Postman interceptor, you can launch them by either going in the Chrome apps tab from the Chrome browser settings, or just simply search by their name in the Spotlight search.

You’ll see this window

Screen Shot 2017-07-23 at 6.22.26 PM

Now, in order to have a first hand knowledge of how to capture API requests and response in Postman, you need to follow this tutorials, which are given by the Postman team itself.

 

I’m not going into the basics of how to capture and test API’s using Postman, since it’s gonna make this post unbelievably long. If you’re some who has almost little or no knowledge of using Postman, then I’d suppose you give at least 2-3 days with these videos and then come back to this post.

  • POSTMAN BDD LIBRARY

Postman BDD Library is one of the awesomest libraries that I have encountered across on the npm installation library.

Although Postman allows you to assert and write tests for your API’s using this tests window (shown below)

2017-07-23_1831

 

Postman’s built-in test framework uses a boolean-flag syntax for testing, like this:

2017-07-23_1848-1

However, these are pretty inadequate in case you want to have a deeper tests involving your JSON-styled response for the API’s (or xml-styled response).
In comes Postman BDD. It let’s your write BDD- styled tests for your API’s in Postman, using Chai-JS syntax .
So, you can group your tests under a common describe section and apply various it statements to check you tests
For, example, if I want to check if the API-response that I am getting is a JSON response, and is a JSON- object, then I would do something like this.
2017-07-23_1848
To start using this, we’re gonna have to install NodeJS, and then the node package manager npm and then we will use npm to install postman-bdd .
Head over to the NodeJS installation page here to install NodeJS.
After this. follow the steps here to install npm .
Once you have both node and npm installed, you can now simply run this to install Postman BDD
npm install postman-bdd
To start using Postman-BDD in your Postman tests, you need to perform these steps mentioned here
  • Send a GET request to the url given below

http://bigstickcarpet.com/postman-bdd/dist/postman-bdd.min.js

 

  • In the same request that you created in Step 1, go to the “Tests” tab and add the following script:

postman.setGlobalVariable('postmanBDD', responseBody)

 

 

  • Now to use Postman-BDD in your individual tests, you need to add this as the first line in your tests tab

eval(globals.postmanBDD)

 

In our next step, we’re going to write some basic tests using Postman-BDD Library.

%d bloggers like this: