Testing REST API’s with Postman and Jenkins – Part 3

In the previous posts, we saw how we can use Postman to run a RESTful API and use BDD library called postman BDD to write custom BDD-styled assertions on the test. We also saw how we can group a bunch of tests together and then can run them all at once using the newman CLI tool.

 

Now, we’ll see how we can run these postman tests using newman and Jenkins, which is a CI tool. This will help us get continuous information on the state of API in our application.

 

Install Jenkins

 

Installing Jenkins is a simple task. Although it differs from platform to platform. Fortunately, I have already written about how you can install Jenkins on a Ubuntu 14.04/16.04 machine here. You can follow this tutorial in case you’re on a Linux/Ubuntu machine.

 

Now, if you’re on a MAC like me, installing Jenkins is a fairly straightforward process all together if you want to install it using homebrew. Installing Jenkins using homebrew is as simple as running a single command from your CLI, provided you’ve already installed Java.

Run

brew install jenkins

and allow jenkins to install.

Once the installation is complete, you need to run

brew services start jenkins

 

and you can head directly to http://localhost:8080/ and you’ll see jenkins installed. Please follow the steps that the installation wizard tells you to do and you shall be having a clean jenkins install in no time.

Now, suppose your port 8080 is already occupied by another application, you can change the port number that you’re using for Jenkins and then host jenkins at that port.

For Mac Systems , to change the port you need a little more configuration

  • Go to the directory where brew installed jenkins . You can find this information by typing

brew info jenkins

which which give you an output like this

2017-07-30_1722

 

Notice that the path where jenkins is installed is

/usr/local/Cellar/jenkins/2.70

  • Use cd to navigate to this directory. List all the directories inside using ls command.
  • You’ll see a directory named libexec. Use cd libexec to navigate inside this directory.
  • Now you need to add a winstone.properties file to add the port name. Type this

sudo vi winstone.properties and hit enter

Once inside, type

httpPort=5499 or whatever the port number you want to launch jenkins. and exit vi editor by pressing esc key and SHIFT+ZZ (two times Z).

  • Now restart the jenkins war file using

java -jar jenkins.war

This will restart jenkins and it will now pick the designated port from winstone.properties file.

 

Now navigate to http://localhost:5499/ to start jenkins on port 5499.

 

New Project In Jenkins

Now to run the API tests, we need to have a new jenkins project. You can create one by following steps here from one of my blogs.

  • Create a new Freestyle project.

screenshot-from-2015-06-26-103208

 

  • Provide a suitable name for the project

screenshot-from-2015-06-26-103221

 

  • Click OK and let it navigate to new page. Here you can configure your project details. Go to the Build section and click on it.

screenshot-from-2015-06-26-103320

  • Select Execute Shell from the options and enter this shell command once the window opens

newman -c Postman_collection.collection -H reports.html -e Postman_environment.environment

And save this project.

  • Now go back to Dashboard. Once there, you need to click on Build Now to start the tests.

screenshot-from-2015-06-26-103413

 

You’ll see that the build fails. The reason is that the workspace that jenkins creates picks the file from a different location, which is

/Users/zac01/.jenkins/workspace/your_project_name

 

Now you need to move your .collection and .environment files from postman to this directory and then build your tests. So copy over the necessary files and then start the build.

 

This will make your jenkins be able to run the newman tests. Now go into the jenkins directory, and then start a build, which would now return the same result as your newman run did.

Start with a new build by clicking on Build Now and see the build executing

2017-07-30_2056

Check out the Console Output to check the out put for a single run. It would be something like this

2017-07-30_2057

 

Now, once you have this build set up, you can set this to run periodically. You can schedule it to run at a specific time of the day by following instructions given here.