logo logo

Running Postman Tests in CI using Newman

Postman Continuous Integration: Running Postman in CI using Newman

We’ve seen how to create and run automated API tests locally, but how should we execute them? Today, many companies have continuous integration (CI) via a build pipeline, where tests are run regularly to validate the quality of the build. If you are trying to run Postman tests on a build system like Jenkins or Travis, you need a few things.

First of all, you need some way for the build system to have access to Postman itself, as well as the collection that you want to run. Second, you need a method to be able to run the tests in some kind of automated way. Why? Because you don’t want to have to log into the build pipeline every time it runs and click on a button to run the tests.

What you need is a command line tool that is easy to install and that can be setup to run the Postman tests that you want in your CI pipeline. Enter Newman.

Postman Tutorial Chapters

  1. Overview – The Ultimate Postman Tutorial for API Testing
  2. Chapter 1 – Getting started with Postman for API Testing
  3. Chapter 2 – Understanding API Authorization Options in Postman
  4. Chapter 3 – Using Postman for Automation Testing
  5. Chapter 4Using Test Scripts in Postman
  6. You’re here → Chapter 5 – Running Postman in CI using Newman
  7. Chapter 6 – Creating Mocks with Postman

What is Newman?

Newman can be used to run Postman collections from the command line and is easy to install in a continuous integration build pipeline. You can also use it locally, so let’s take a look at how that works.

Newman comes as an npm package built in Node.js. This means that you need to have Node.js installed to install Newman. If do not yet have Node.js installed on your machine you can download and install it from here. Once you have Node.js installed, installing Newman is a cinch. Open up a command prompt and type in the following command:

npm install -g newman

❗ The -g in the command means that Newman will be installed globally. The package manager will download and install everything that you need in order to run Newman.

Running Tests with Newman

The easiest way to run tests with Newman is to run a collection. In order to do that, first go into Postman and find the collection that you want to run. Click on the more actions menu (3 dots) beside the collection and choose the export option.

Postman Tutorial Chapter 5 - Running Tests with Newman

You can use the recommended format. Make sure you note the file name and location. Now, return to the command prompt and navigate to the folder that contains the exported collection. You can then run the collection in Newman with this command:

newman run <collection-name>

where <collection-name> is the name of the collection that you exported from Postman. Hit enter and the collection will run by Newman and it will give you a nice report at the end.

Running Newman in CI

Newman can be used to run tests in continuous integration builds. It has integrations for many of the common CI platforms, including Jenkins and Travis. The Postman documentation has information on how to integrate with several of the popular CI systems. The exact steps are going to vary from platform to platform, but in essence, they will be the same thing that you did when you setup and ran Newman locally. You will need to have an install step where you install Newman with npm and you will need to have a run step where you use Newman to run the test collections you are interested in. In order to do this you will need to include the test collection files in your build system (for example, in a folder named “tests”) and point Newman to them.

Summary

If your company is using a build system to build and test an application, you can easily integrate API tests into the build system by using Newman and integrate your automated API testing in CI. This process makes it easier to determine if an API has bugs before you ship it to your clients. Running API tests in CI is simple and powerful when you use Postman and Newman together.

In the next and final chapter, we will look at API mocking and how you can use it in Postman.

💥 TAKE ME TO CHAPTER #6 💥

About the author

Dave Westerveld

Dave Westerveld is an experienced tester who has been involved in various aspects of the testing role. As a strong exploratory tester, he has learned how to leverage many different tools to enhance his testing powers. He has also been involved in many automation projects including building out new automation frameworks. In addition he has helped transition several large and expensive automation suites into lighter weight, higher value systems. A speaker at several conferences, Dave also blogs about his thoughts and experiences at offbeattesting.com

Comments

0 3 comments
  • Srinivas Kadiyala July 30, 2020, 2:26 am

    The command: npm run returns error message.
    C:\development\jsontest>npm run JsonPlaceHolder.postman_collection.json
    npm ERR! code ENOENT
    npm ERR! syscall open
    npm ERR! path C:\development\jsontest\package.json
    npm ERR! errno -4058
    npm ERR! enoent ENOENT: no such file or directory, open ‘C:\development\jsontest\package.json’
    npm ERR! enoent This is related to npm not being able to find a file.
    npm ERR! enoent

    npm ERR! A complete log of this run can be found in:
    npm ERR! C:\Users\skadiyala\AppData\Roaming\npm-cache\_logs\2020-07-30T02_26_23_685Z-debug.log

    I think the command, needs to be:
    newman run JsonPlaceHolder.postman_collection.json

Leave a Reply

FacebookLinkedInTwitterEmail