Integrating Rest Assured with Jenkins and Allure reports.

Reading Time: 4 minutes

Hi folks,

In this blog, we will be exploring how to integrate Jenkins with our Rest Assured test project. And as an add on we will try to publish the Allure reports through Allure Jenkins plugin as well. So let’s get started.

Why are we doing this?

The main aim here is to integrate Rest Assured with CI/CD through Jenkins and to publish the test reports along with every successful build through Allure report plugin.

How to setup Jenkins?

First of all, we need to download Jenkins to our local system. It is recommended that we should download the generic java package (.war) file as it can be executed on almost every OS. We can download it from here, https://www.jenkins.io/download/ .

After downloading it, open the terminal and open the directory where this .war file is present. Afterwards, execute the command

java -jar jenkins.war

Now while it is running, open this address in our web browser http://localhost:8080. Moving forward, something like this will appear on our screen.

We can find the password in our terminal and on the location specified in the image above. Now, we have to install the plugins, it is always safe to install the suggested one’s. We can do so by clicking on it, as specified in the image below.

It will take some time to install these plugins, so please be patient. After the plugin installation, we will encounter something like the image given below.

Adding Allure Plugin

After setting these fields, we will have to manage our plugins. We can do so by clicking on the Manage Jenkins option on the left side of our dashboard and after that we would have to click Manage Plugins option. We have to have two additional plugins before creating a new job. These are reporting plugins to be exact. First one is HTML Publisher and the second one is Allure Jenkins Plugin. Allure Jenkins plugins integrates Allure reporting tool into Jenkins.

Moving on, after installing these plugins go back to the main dashboard click on Manage Jenkins> Global Tool Configuration. At the end of the page, we will see an option named as Allure Commandline. Fill out the fields that needs to be filled and select the maven version as well. It will look something like this

Just click on apply and save the chnages.

Adding your rest assured project to Jenkins.

To do so, we would have to create a new job. Go back to the main dashboard click on “Create a new Job“. In this blog, we will call out the mandatory fields that needs to be filled while creating a job, the rest of the things are optional.

Firstly, we have to specify the VCS that we are using, for example, here we are using GitHub. Select Git in the Source Code Management section and fill out the repository link and branch(if any).

Moving forward, we need to specify the Build Trigger Steps. Since it is a maven project we will have to select “Invoke top-level Maven targets“.

Then, we would have to configure the Build Environment , here we have to specify the path to our pom.xml file and our goal which is “clean test“.

Lastly, only post build actions are left. Here, we have to select “Allure reports” which will get generated after every successful build.

That’s it. Now we just have to build our job and we can do so by clicking on the “Build Now” button on the left side of the dashboard. After a successful build, it would look something like this.

Sample Allure report

If someone need a sample rest-assured test script. Here you go!

package com.knoldus;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.json.simple.*;
import java.io.FileNotFoundException;

public class SampleTest {
    @Test
        //TestNG annotation
    void postNewEmployees() throws FileNotFoundException {
        RestAssured.baseURI = "http://dummy.restapiexample.com";
        RequestSpecification httpRequest = RestAssured.given();

        //Here we created data through json object that we can send along with POST request
        JSONObject requestParams = new JSONObject();
        requestParams.put("name", "abc");
        requestParams.put("age", "10");
        requestParams.put("salary", "10000");

        // specifying headers here, stating that the request body is json
        httpRequest.header("Content-Type", "application/json");

        // add the json body to the request payload
        httpRequest.body(requestParams.toJSONString());

        // Sending POST request
        Response response = httpRequest.request(Method.POST, "/api/v1/create");

        //capturing response body to perform validations
        String responseBody = response.getBody().asString();
        Reporter.log(responseBody); // to log the response

        // to capture response code through getStatusCode method.
        int status = response.getStatusCode();

        Assert.assertEquals(status, 200);
    }
}

Thank you, I hope you may have found this useful.

Refrences

https://www.softwaretestinghelp.com/automate-api-requests-using-rest-assured/

https://www.qaautomation.co.in/2019/07/automated-api-testing-with-jenkins.html


Knoldus-blog-footer-image

Written by 

Sparsh is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.

1 thought on “Integrating Rest Assured with Jenkins and Allure reports.4 min read

  1. Is it possible to have Jenkins pass a variable to the rests-assured tests to specify the environment for tests to validate?

Comments are closed.

%d bloggers like this: