At MSolution.IO, we have the opportunity to write REST APIs for multiple projects. A few months ago, we decided to use Jenkins to perform tests on our different products and clients projects.
Jenkins is an open-source continuous integration tool written in Java. We are running it on an Ubuntu server with nginx http server. Builds can be started by various means, most of ours are run each time a commit is pushed in our bitbucket repository.
Continuous integration comes along with testing and will make sure that nothing recently pushed causes regressions on the API.
Below is a guide to setup this continuous integration configuration in a specific case of a REST API testing. We’ll start by installing Jenkins on an Ubuntu 14.04 server and we’ll continue with the REST API testing.
Install Jenkins on Ubuntu 14.04
Jenkins requires Java7 or above to function. Java8 is prefered.
sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-get update sudo apt-get install openjdk-8-jre
To make sure that Java has been correctly installed on your server you can simply type :
Then you can install Jenkins with the following commands :
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary > /etc/apt/sources.list.d/jenkins.list' sudo apt-get update sudo apt-get install jenkins
By default, it will run on port 8080. You can find how to setup your nginx proxy for port 80 -> 8080 in this documentation
After a few seconds, you’ll see the web UI on your server:
Setup a Jenkins client
Now that jenkins is installed on your server, you’ll want to setup a client where your API is running.
Your client will also need Java 7 or above, install it the same way as described in 1.1.
Then on the web UI go on “Manage Jenkins” -> “Manage Nodes” -> “New Nodes”
Give it a name, select “Dumb Slave” and press OK
Configure your client that way.
You’ll need to create a Jenkins user in the sudo group on it, and don’t forget to add your credentials.
Now Jenkins and its agents are up and running. Let’s go to Postman. Postman is a request builder, it allows you to create and send any HTTP request. The request builder lets you create almost any kind of request quickly. The four parts of an HTTP request are the URL, method, headers, and the body. Postman gives you tools to work with each of these parts.
For example, Postman will translate the URL
http://www.msolution.io/test/index.html into the following request message:
GET /test/index.html HTTP/1.1 Host: www.msolution.io Accept: image/gif, image/jpeg, */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (Linux; Android 5.1.1; A0001 Build/LMY48Y) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.95 Mobile Safari/537.36 (blank line)
In this example, we will explain how to create your first request using Postman. We will write our own test case to validate response data, response times, and more.
Postman works as a Chrome extension, you can simply download it from the official website.
Configure your first request
Once Postman has been installed, you can start writing your own requests. In this example we’ll see how to send a Post request in order to login to the API and save the token we get in response, to be able to use it later.
Start by creating a new collection, name it, and press “OK”
Then you can start adding your login Post request by typing the URL of your API.
Change the Headers by what you need. In our case : Content-Type : application/json.
And write your parameters in the Body Tab.
Then you can add your tests that Jenkins will verify. In our case, we want to make sure that the API returns the status code 200, and we want to keep the token in response for other requests.
You can now save your first request.
After you can test your request by running the collection
Once this is done, you can download your collection and put the json file on your Jenkins client.
Newman is simply a companion line command tool for Postman. It can be installed easily through the Node.js package manager, NPM, in a few seconds for all major operating systems
Now, we want to install Newman on our Jenkins client. Newman will be run directly by Jenkins and will catch the tests we made with Postman.
As we said before, we need to install Node.js and NPM to install Newman:
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs sudo apt-get install -y build-essential npm install -g [email protected]
At the time of this article cause the last version of postman doesn’t have a corresponding newman stable version yet. So far we didn’t discover any issue with this beta version.
Run your Postman Collection using Newman
Still on your Jenkins client, you can make sure that your collection is working fine by running it using Newman.
Create and run your Jenkins job
Everything is now ready to be setup on Jenkins.
Create your new job and configure it this way:
Then you just have to build your jobs, and verify the console output looks like this :
In this article we’ve seen how to setup Continuous Integration for your REST API using Jenkins, Postman and Newman. We made all our tests on Ubuntu 14.04.
Continuous Integration with Jenkins allows you to create and configure jobs, specify when they are being executed and get notified when they fail. Jenkins is a very flexible tool where you can do all kinds of things with these jobs. In this article you saw how we use it for our back-ends integrations but it can definitely work for front-end uses or any management jobs as backup for example also.