NashTech Insights

Establishing a Continuous Integration (CI) pipeline for Rest Assured tests using git-lab for API testing

Anshita Mathur
Anshita Mathur
Table of Contents

In this blog, we’ll look at utilizing GitLab, a popular web-based Git repository management tool, to set up a CI pipeline for Rest Assured test. 

In modern software development, continuous integration (CI) is an essential technique that promotes code stability and quality. By automating the build, test, and deployment procedures, CI pipelines assist teams in accelerating the delivery of high-quality software. Additionally, they assist teams in finding bugs earlier and reducing the possibility of integration issues. 

Prerequisites: 

Make sure you meet the requirements listed below in place before starting the setup process: 

  1. A GitLab account (either self-hosted or utilizing gitlab.com). 
  1. A Rest Assured test suite with a valid Maven project structure. 
  1. Familiarity with basic Git commands and GitLab concepts. 
  1. Ensure that runners are available for executing your CI/CD jobs in GitLab.

For a list of available runners:  

  • Expand Runners by selecting Settings > CI/CD. 
  • You have a runner accessible to process your jobs if you have at least one running with a green circle next to it and at least one of them active. 

How to Build or set up a CI/CD Pipeline for Rest Assured Tests? 

Step 1: Create a GitLab Repository 

  • To get started, begin by creating a fresh GitLab repository that will serve as a dedicated container for your Rest Assured tests. You can create this repository from scratch or repurpose an existing one to fulfill this objective.

Step 2: Configure Your Local Environment 

Ensure that you correctly install all the required tools in your local development environment. It includes: 

  • Git: Install Git and establish your personal Git configuration locally. 
  • Maven: Install Maven to start building Java projects automatically. 
  • JDK: Set up the Java Development Kit (JDK) that is appropriate for your project. 

Step 3: Add a .gitlab-ci.yml File 

  • Your CI pipeline’s primary configuration file is .gitlab-ci.yml. Your GitLab project’s root directory should contain this file and is where you define the stages and jobs for your pipeline. 

The following can be specified in the .gitlab-ci.yml file: 

  • The scripts you want to execute. 
  • You want to add more configuration files and templates. 
  • Caches and dependencies. 
  • Both the commands you want to execute sequentially and those you wish to execute simultaneously. 
  • the location where your application will be deployed. 
  • Whether you want to manually trigger any of the scripts or have them run automatically. 

For example: 

  • In this configuration, the CI/CD process is divided into two distinct stages, namely “build” and “test”. Each stage serves a specific purpose in the overall pipeline. 
  • The “build” stage focuses on compiling the code, which involves transforming the human-readable source code into executable machine code. This stage typically involves tasks such as compiling dependencies, generating binaries, and creating artifacts. Its main objective is to ensure that the codebase is ready for further testing and deployment. 
  • On the other hand, the “test” stage is responsible for executing Rest Assured tests using Maven. Rest Assured is a popular Java library used for testing RESTful APIs. Maven, a build automation tool, manages the project’s dependencies, compiles the code, and executes the defined tests. This stage is crucial for validating the functionality and correctness of the application’s RESTful API endpoints. 
  • Splitting the CI/CD process into these two stages enables a clear separation of concerns and promotes efficient development practices. The “build” stage ensures the code is successfully compiled, while the “test” stage verifies the API functionality, contributing to the overall quality and reliability of the software. 

For how to configure GitLab Runner, you can take a reference from https://docs.gitlab.com/ee/ci/quick_start/

Organizing Jobs and Stages-

  • The scripts within the CI/CD configuration are organized into individual jobs that form part of a broader pipeline. These jobs can be grouped into stages, which dictate their execution order. It is crucial to include at least one visible job within the CI/CD configuration. 

Logical Sequence of Jobs-

  • To ensure the proper functioning of your application and alignment with the desired tests, it is advisable to arrange your jobs in a logical sequence. It may be helpful to conceptualize the scripts added to jobs as analogous to executing command-line interface (CLI) commands on your computer. 

Execution of Scripts with GitLab Runner-

  • Once you have added the .gitlab-ci.yml file to your repository, GitLab recognizes its existence, and a software application called GitLab Runner proceeds to run the scripts specified within the jobs.

Real-Time Updates and Monitoring-

  • During this process, when you monitor the terminal, you will observe that GitLab CI/CD not only executes the jobs but also provides real-time updates on the ongoing operations: 
  • For your app, you develop the strategy, and GitLab implements it by running the pipeline as you’ve specified. GitLab also displays the status of your pipeline:

Step 4: Push and Trigger the Pipeline: 

  • To start the CI pipeline, commit and push your modifications to GitLab. The defined stages and jobs will begin running on GitLab automatically after it recognizes the .gitlab-ci.yml file. The GitLab interface allows you to keep tabs on the development and review the pipeline logs. 

Step 5: Configure Test Reports: 

  • You can set up GitLab to store and show the test reports produced by your Rest Assured tests so that you can examine comprehensive test reports and keep track of test outcomes. GitLab offers several options for doing this, including the use of JUnit XML reports or customized report generators. Afterward, add the configuration required to create and save test results to your .gitlab-ci.yml file. 

Step 6: Handle Build and Test Failures:  

  • When a build or test fails, GitLab will show the failure in the pipeline view, and you can access the comprehensive logs to figure out the problem. Handle failures promptly by diagnosing the problematic code or tests, resolving the issues, and pushing the updated code to initiate a fresh pipeline run.
  • Basically, you will be able to observe the various stages that you have defined and determine if each stage has succeeded or not. Clicking on the specific stages allows you to access a detailed overview of the jobs executed within each stage.

Step 7: Further Pipeline Enhancements: 

  • GitLab offers a range of features that can enhance your CI pipeline, providing opportunities for improvement. These include parallel test execution, analysis of code coverage, deployment to various environments, and integration with external tools like SonarQube for static code analysis.
    We recommend exploring GitLab’s documentation and adapting your workflow to suit the specific requirements of your project. By leveraging these capabilities, you can optimize your CI pipeline for maximum efficiency and effectiveness. 

Conclusion: 

In conclusion, By implementing a CI pipeline for Rest Assured tests using GitLab, you can significantly improve your software development process through automated testing and deployment. This blog provides detailed instructions on seamlessly integrating Rest Assured tests into your CI workflow, guaranteeing the reliability and excellence of your APIs. Unlock the full potential of Rest Assured for efficient API testing by harnessing the power of CI. 

However, as your team grows, the complexity of managing code changes across multiple staging environments increases, leading to a higher risk of errors. In such scenarios, the CI/CD pipeline becomes essential, serving as a critical component in ensuring the smooth migration of code changes between different environments. 

References: 

Anshita Mathur

Anshita Mathur

I am Software Consultant - QA at Nashtech Global. I have completed my B.Tech from Jodhpur Institute of Engineering and Technology. I am familiar with the core concepts of Manual and Automation Testing and tools such as Postman, Selenium, Appium, and Gatling. I am looking forward to roles that will help me realize my potential by exploring the various aspects of this field.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: