Testing with Postman

501 VIEWS

·

Many apps we use daily rely heavily on services delivered via an Application Programming Interface (API): search, messaging, mapping, banking, eCommerce, storage, and ads, to mention a few. Without these APIs, most apps we use are nothing but nice-looking (if that can even be said) user interface designs.

Building, integrating, and testing APIs has rapidly become a required skill set for software developers. Like most other things, there are tools that help developers smoothly work with APIs. I have already covered REST-Assured, used for testing and validating RESTful services in Java. Again, I’ll cover another API tool with even more features and a supporting user interface—this time, Postman.

Postman is the most widely used API Development Environment (ADE). Postman is used by millions of developers to access over 100 million APIs each month. It contains essential features for every stage of API development, integration, and testing: API mocking, documentation, monitoring, requests, collections, etc. This post will only cover a handful of these features.

Installation

Postman is built with the Electron framework and runs on Windows, Mac, and Linux. Linux users should note that the app is available as a Snap. A Chrome app version of Postman is available; however, its features are quite limited, and its use is discouraged. Head to the download page to get the appropriate version.

You don’t need an account to use Postman, although an account gives you extra features like Sync and Workspace. A Free account should cover the most basic needs; there are Pro and Enterprise accounts for more support.

Requests

All API requests (POST, PUT, GET, PATCH, DELETE) can be made without writing a single line of code. Parameters to every request are entered in the Params drop-down (request headers as well). Clicking Send/Send and Download will make the request and, in the latter case, will allow you to save the response locally. Postman supports a wide range of authentication mechanisms, including AWS signature, OAuth (1 and 2), Bearer Token, and Hawk Authentication. A plus is that SOAP requests are supported.

Vital request information such as request time, status code, headers, cookies, and request size is displayed nicely in a panel. Request response can be formatted and copied to the clipboard in a single click. The search feature comes in handy when the response body is very long.

Tests

Postman comes with a JavaScript execution environment for writing tests, called Postman Sandbox. As the name suggests, the Sandbox is a sandboxed environment for only tests. Tests in Postman can be written for two stages: before a request is sent and after a response is received. A test to be executed before requests is known as a pre-request script. A test written to be executed after a request is simply called a test script.

The results of tests are shown in the Test Results section of the response panel.

Commonly used test snippets are displayed at the sides once you click on Pre-req or Tests under each request. This comes in handy and can be edited to meet your needs. Console outputs are captured in the Postman Console, which can be accessed by clicking on the console icon at the bottom-right section of the window.

Writing Tests

We’ll explore a status assertion test script and how we can construct our own test scripts.

– pm is a JavaScript object and the entry point for scripts. It allows access to all information about a script.
– test is a function of the form test (testString: String, functionToRun: function).
– response is an object with information on received response and has some of the following properties: headers, responseTime, and code. To get the header list, status code, and response time (in ms) of a response, simply log:

The functions, objects, and properties of the pm object allow you to build complex and meaningful tests. You can read about all these in the API reference.

Closing Thoughts

Postman makes working with APIs seamless. It offers the most complete features. While only a handful of Postman features are covered here, this should be a starting point to its adoption, if you’re not already using it. You should also check out Newman, Postman’s CLI.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu