Running automated tests with GitHub Actions + Maven + TestNG

Running automated tests with GitHub Actions + Maven + TestNG

Introduction

Use case

In today's fast-paced software development world, automated testing is essential for ensuring the quality and reliability of applications. It's essential to integrate execution of automation test cases with CI (Continuous Integration) pipeline to run tests after each change to identify and fix bugs early in the development process.

GitHub Actions

GitHub Actions is a cloud-based platform that makes it easy to create and manage automated testing workflows.

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your software development lifecycle. It enables you to build, test, and deploy your code automatically, ensuring consistent quality and efficiency throughout your development process.

Below is component of GitHub actions

  • Workflow - A workflow defines a sequence of steps that are executed in response to a specific event. These events can range from code changes (push or pull requests) to external triggers, such as scheduled intervals or API calls. Workflow is created in yaml file

  • Triggers - Triggers are the events that initiate the execution of a workflow. GitHub Actions supports a wide range of triggers, including:

    • Push events: When code is pushed to a repository

    • Pull request events: When a pull request is created, updated, or merged

    • Schedule events: Executing a workflow at specific times or intervals

    • Manual events: Triggering a workflow manually from the repository

    • Webhook events: Receiving events from external sources, such as other applications or services

  • Jobs - A workflow is composed of one or more jobs. Each job represents a specific task within the automation process, such as building, testing, or deploying code. Jobs can be executed in parallel or sequentially, depending on the workflow configuration.

  • Steps - Steps are the fundamental units of execution within a job. Each step performs a specific action, such as running a command, installing software, or publishing artifacts. Steps can be chained together to form a complex automation task.

Example

Pricing → https://github.com/pricing

I am using Free Plan which has 2000 minutes/month


Setting Up the Environment

To demonstrate the use of GitHub Actions to run automated tests, I am implementing below flow

Image Link --> https://github.com/sksingh329/api-automation-framework/wiki/Article-Image-%E2%80%90-Running-automated-tests-with-GitHub-Actions---Maven---TestNG

For source code please refer GitHub repo --> https://github.com/sksingh329/api-automation-framework

GitHub Workflow

 name: Run Maven Test

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  tests:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'
        cache: maven

    - name: Run Go Rest Smoke
      run: mvn clean test -DsuiteXmlFile=gorest/gorest_testng_smoke.xml


    - name: Run Go Rest Regression
      run: mvn clean test -DsuiteXmlFile=gorest/gorest_testng_regression.xml


    - name: Upload Extent HTML report
      uses: actions/upload-artifact@v3
      if: always()
      with:
        name: go-rest-report
        path: reports/extent/report_*.html

In the step Upload Extent HTML report step,

  • uploading html report as an artifact

  • added an condition to run this step as always() as we need to have report even if previous steps has failed.


Result

Image Link --> https://github.com/sksingh329/api-automation-framework/wiki/Article-Image-%E2%80%90-Running-automated-tests-with-GitHub-Actions---Maven---TestNG

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!