Integrating automated API tests with Jenkins

  • Newman
  • App

Postman contains a full-featured testing sandbox that lets you write and execute Javascript based tests for your API. You can then hook up Postman with your build system using Newman, our command line companion. Newman is Postman’s Collection Runner engine that sends API requests, receives the response and then runs your tests against the response.

Newman and Jenkins are a perfect match. Lets start setting this up. We are using Ubuntu as a target OS as in most cases your CI server would be running on a remote Linux machine.

1. Install Jenkins

2. Install NodeJS and npm. Newman is written in NodeJS and we distribute the official copy through npm. Install nodejs and npm for Linux here

3. Install Newman through

npm install -g newman

This would set up newman as a command line tool in Ubuntu.

Run a sample Postman Collection. We are assuming that you already have a Postman Collection with some tests. This is what the output looks in Postman’s Collection Runner.

Some of my tests are failing intentionally in the screenshot so we can show you the instructions for troubleshooting.

4. Run this collection inside newman, using the command newman run jenkins_demo.postman_collection. If everything is set up nicely, you should see the output below.

Jenkins exposes an interface at http://localhost:8080.

5. Create a new job by clicking on the “New Item” link on the left sidebar > Select a “Freestyle Project” from the options > Name your project.

6. Add a build step in the project. The build step executes a Shell command (for linux and Mac OS) and execute Windows batch command (for windows).

The command is

newman run jenkins_demo.postman_collection.

Click the save button to finish creating the project.

7. Run this build test manually by clicking on the “Build Now” link in the sidebar.

Jenkins indicates that the build has failed with a red dot in the title. We can check why with the console output from newman.

8. Click on the “Console Output” link in the sidebar to see what newman returned.

9. Fix these tests inside Postman and then try again.

You can move on once you see green pass icons for all your tests like the screenshot above.

Jenkins indicates that the build succeeded with a blue ball.

10. To set up the frequency with which Jenkins runs newman, click on “Configure project” in the main project window and then scroll down.=. The syntax for setting the frequency is H/(number of minutes after which there’s a Jenkins build trigger) * * * *

Jenkins will now run newman at your desired frequency and will tell you whether the build failed or succeeded. In a bigger set up, newman will be part of your build process and probably not the entire process. You can set up notifications and customise Jenkins as per your needs.

You can use a wide variety of other configurations to make your collection more dynamic.