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
- Overview – The Ultimate Postman Tutorial for API Testing
- Chapter 1 – Getting started with Postman for API Testing
- Chapter 2 – Understanding API Authorization Options in Postman
- Chapter 3 – Using Postman for Automation Testing
- Chapter 4 – Using Test Scripts in Postman
- You’re here → Chapter 5 – Running Postman in CI using Newman
- 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.
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.
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
Thanks for pointing this out. You are totally correct that there is a typo here and it should say newman and not npm. I will get that updated ASAP. Thanks for pointing it out!
Thank you 🙂