How to run Rest API tests with GitHub Actions

Last Updated On

HOME

This tutorial explains the steps to create a GitHub Action for the Java Rest API tests and execute the tests in that workflow.

Table of Contents

Why GitHub?

GitHub serves as a collaborative platform that supports version control, code collaboration, automated testing, and issue tracking, all of which are crucial elements in the software testing process. It promotes transparency, collaboration, and efficiency in the development and testing workflows.

CI/CD pipelines have contributed to the success of the DevOps cycle in all software development projects. This is a holistic process that bridges development and operations. Continuous integration helps development teams deploy code efficiently, and continuous delivery automates code deployment.

Implementation Steps

Step 1 – Create GitHub Actions and Workflows

I have a repository available in GitHub – RestAssured_TestNG_Demo as shown in the below image. Go to the “Actions” tab.  Click on the “Actions” tab.

Step 2 – Select the type of Actions

You will see that GitHub recommends Actions depending on the project. In our case, it is recommending actions suitable for a Java project. I have selected the “Java with Maven” option as my project is built in Maven.

Step 3 – Generation of Sample pipeline

If you choose an existing option, it will automatically generate a .yaml for the project as shown below.

We will replace the current workflow with the following yml file as shown below:

name: Rest API Tests using Rest Assured with TestNG
 
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
 
jobs:
  build:
 
    runs-on: ubuntu-latest
 
    steps:
    - uses: actions/checkout@v4
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: maven
     
    - name: Test Execution
      run: mvn clean test
       
    - name: Test Report Generation
      uses: actions/upload-artifact@v4
      if: success() || failure()
      with:
          name: TestNG Report                 # Name of the folder
          path: target/surefire-reports/      # Path to test results

Step 4 – Commit the changes

After the changes, hit the “Start Commit” button.

This will give the option to add a description for the commit. It will also enable the user to commit either to the main branch or commit to any other branch that exists in the project. Click on the “Commit new file” button to set up the workflow file.

Step 5 – Verify that the workflow is running

Next, head over to the “Actions” tab, and you will see your YAML workflow file present under the tab. The yellow sign represents that the job is in the queue.

In Progress – When the job starts building and running, you will see the status change from “Queued” to “in progress”.

Passed – If the build is successful, you will see a green tick mark. 

Click on the workflow and the below screen is displayed. It shows the status of the run of the workflow, the total time taken to run the workflow, and the name of the .yml file.

Below shows all the steps of the workflow.

The complete code can be found here on GitHub – vibssingh/RestAssured_TestNG_Demo.

Congratulations! We just created our CI workflow for running our Rest API test cases.

Rest Assured Tutorials

HOME

RestAssured is a Java-based library that is used to test RESTful Web Services. REST-assured was designed to simplify the testing and validation of REST APIs and is highly influenced by testing techniques used in dynamic languages such as Ruby and Groovy.

Chapter 1 Introduction to Rest Assured
Chapter 2 Setup Basic REST Assured Maven Project In Eclipse IDE
Chapter 3 How to test GET Request using Rest Assured
Chapter 4 How to test POST Request using Rest Assured
Chapter 5 How to test PUT Request using Rest Assured
Chapter 6 How to test DELETE Request using Rest Assured
Chapter 7 How to test PATCH Request using Rest Assured
Chapter 8 How to test POST request from JSON Object in Rest Assured
Chapter 9 How to test POST JSON Object request using Java Map in Rest Assured
Chapter 10 How to create JSON Array Request Body – org.json
Chapter 11 How to print pretty JSON using org.json library in Java?
Chapter 12 Assertion of JSON in Rest Assured using Hamcrest
Chapter 13 Extraction from JSON in Rest Assured
Chapter 14 How To Send A JSON/XML File As Payload To Request using Rest Assured
Chapter 15 Logging in Rest Assured
Chapter 16 How to test SOAP Services using Rest Assured
Chapter 17 How to send basic authentication credentials in Rest Assured
Chapter 18 How to add Content Type to request in Rest Assured
Chapter 19 How to verify the response time of a request in Rest Assured?
Chapter 20 How to verify JSON response headers in Rest Assured? 
Chapter 21 How to perform multiple assertions in Rest Assured? 
Chapter 22 How to handle HTTP Query Parameters using REST Assured
Chapter 23 How to validate JSON body in Rest Assured?
Chapter 24 Compare JSON Objects using JSONAssert Library
Chapter 25 Compare JSON Arrays using JSONAssert Library
Chapter 26 How to blacklist headers in Rest Assured
Chapter 27 How to pass authorization token in header in Rest assured? – NEW
Chapter 28 Generating and Using Access Tokens in a Rest API: A Complete Guide – NEW

JSON Manipulation

Chapter 1 Serialization – How to create JSON Payload from Java Object – Jackson API
Chapter 2 Deserialization – How to convert JSON to Java Object using Jackson API
Chapter 3 How to create JSON Array Payload using POJO – Jackson API
Chapter 4 How to create Nested JSON Object using POJO – Jackson API
Chapter 5 Rest Assured – How to test JSON Request using Jackson API
Chapter 6 Rest Assured – @JsonIgnore Annotation in Jackson API
Chapter 7 Rest Assured – @JsonIgnoreProperties in Jackson
Chapter 8 Serialization – How to convert Map to JSON string using Jackson API
Chapter 9 Deserialization – How to convert JSON to Map using Jackson API

XML Manipulations

Chapter 1 How to parse XML in Java
Chapter 2 How to retrieve XML Child Nodes in Java
Chapter 3 Serialization – How to convert Java Objects to XML using Jackson API
Chapter 4 Deserialization – How to convert XML to Java Objects using Jackson API
Chapter 5 Jackson Annotations for XML – JacksonXmlRootElement
Chapter 6 Marsalling – How to convert Java Objects to XML using JAXB
Chapter 7 UnMarshalling- How to convert XML to Java Objects using JAXB
Chapter 8 @XmlElementWrapper Annotation for XML – JAXB
Chapter 9 XML Marshalling – Convert Java objects to XML using JAXB Version 3
Chapter 10 XML Unmarshalling – Convert XML to Java objects using JAXB Version 3

Gradle

Chapter 1 Setup Basic REST Assured Gradle Project In Eclipse IDE

Frameworks

Chapter 1 Integration of REST Assured with TestNG
Chapter 2 Integration of REST Assured with JUnit4
Chapter 3 Integration of REST Assured with JUnit5
Chapter 4 Serenity BDD with Cucumber and Rest Assured
Chapter 5 Serenity BDD with Cucumber and Rest Assured in Gradle
Chapter 6 How To Create Gradle Project with Cucumber to test Rest API
Chapter 7 Rest API Test in Cucumber and JUnit4
Chapter 8 API Automation with REST Assured, Cucumber and TestNG – NEW

GitHub Tutorials

HOME

GitHub is a web-based version control system and collaboration platform for developers. It is the centre around which all things involving git revolve. GitHub allows software developers and engineers to create remote, public-facing repositories on the cloud for free. Once you’ve set up a repository on GitHub, you can copy it to your device, add and modify files locally, and then “push” your changes back to the repository where your changes are displayed for the public.

Chapter 1 How to create a new repository on GitHub
Chapter 2 How to delete a repository on GitHub
Chapter 3 How to Fork a GitHub repository
Chapter 4 How to clone a project from GitHub using Eclipse
Chapter 5 How to copy code from another version control to GitHub – GitHub Importer
Chapter 6 What is pull request in GitHub?
Chapter 7 How to run Selenium tests with GitHub Actions
Chapter 8 How to upload Artifacts in GitHub
Chapter 9 How to run Gradle tests with GitHub Actions
Chapter 10 How to run SpringBoot tests with GitHub Actions
Chapter 11 How to run Serenity tests with GitHub Actions – NEW
Chapter 12 How to run Rest API tests with GitHub Actions – NEW
Chapter 13 How to host Extent Report on GitHub Pages with Github Actions – NEW