Automated API tests with Postman CLI

Overview

On a project I was working on, I recently researched how to apply Postman CLI to an Azure DevOps pipeline. Postman is a powerful tool to run both manual tests and ultimately to have these tests performed automatically. For the automated tests I have actually always used Newman until now, but recently I also used Postman CLI. I believe that Postman CLI setup is easier. I assume that you have a basic understanding of Postman.

Major differences between Newman and Postman CLI

Basically, Postman provides 2 ways to perform automated tests. You can use Newman via the command line to perform the tests. Newman can be easily installed via npm. In the most basic situation you can then execute the tests with newman run <collection-name>.json. For reporting there is a command line option, e.g. -r html --reporter-html-template for HTML reports.

Postman CLI on the other hand is more cloud focused. To use execute Postman collections with Postman CLI the first thing you need to do is authenticate. Next, you need the unique identifier of the Postman collection to be run and optionally the UID of the Postman environment.

Prerequisites

Step 1 - Generate API key

Open de API Keys page and hit the Orange button Generate API Key, give it a logical name and copy the key to a safe place.

Step 2 - Install Postman CLI

For this tutorial I'm using a Linux installation. In that case you can visit the Postman CLI installation page and copy the install command to run the installation. Once installed you get a message with The Postman CLI has been installed.

Step 3 - Create a basic Postman collection

Create a simple Postman collection with one HTTP GET request. Follow the documentation here if you need instructions. In my example I will use a DummyJSON url to fetch sample product data. Hit the blue Send button to verify that the HTTP response is 200 OK with a JSON response body. Don't forget to Save!

Postman GET request

Step 4 - Collect the collection UID

Click the collection name you created and on the right click on the Info button where a new window will open. Copy the UID of the collection.

Postman collection uid

Step 5 - Execute the test

Let's try to execute the Postman collection with Postman CLI:

1postman login --with-api-key <your api key here>
2postman collection run <your collection uid here>

If the test succeeds you will get a testreport on the console and an URL to view the testreport in the Postman cloud!

Postman test