Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up API Automation Testing with Jenkins Pipelines

Tech 2

Prerequisite Plugins

To facilitate a robust API testing workflow, ensure the following Jenkins plugins are enstalled via the Plugin Manager:

  • Pipeline: Essential for defining CI/CD processes as code.
  • Git: Enables the workspace to pull source code from remote repositories.
  • Allure Jenkins Plugin: Used for generating and visualizing comprehensive test reports.
  • Email Extension / HTTP Request: Useful for sending notifications to messaging platforms like Slack or WeChat.

Environment Configuration

Before executing test scripts, verify the global tool configurations in Manage Jenkins > Global Tool Configuration:

  • JDK: Configure the Java path required for Jenkins and certain reporting tools.
  • Python/Node.js: Ensure the specific runtime for your test framwork is mapped to the system PATH so it can be invoked via shell commands.

Creating the Jenkins Pipeline

Create a new "Pipeline" item and use the following declarative script to manage the lifecycle of your API automation project.

pipeline {
    agent any

    environment {
        VENV_NAME = 'api_test_venv'
        RESULTS_DIR = 'test-reports'
    }

    triggers {
        // Poll SCM for changes every 15 minutes
        pollSCM('H/15 * * * *')
        // Schedule a full suite execution daily at midnight
        cron('0 0 * * *')
    }

    stages {
        stage('Pull Source Code') {
            steps {
                git branch: 'master', url: 'https://github.com/tech-org/api-automation-suite.git'
            }
        }

        stage('Initialize Environment') {
            steps {
                sh """
                    python3 -m venv ${VENV_NAME}
                    . ${VENV_NAME}/bin/activate
                    pip install -r requirements.txt
                """
            }
        }

        stage('Execute Test Suite') {
            steps {
                sh """
                    . ${VENV_NAME}/bin/activate
                    pytest tests/ --alluredir=${RESULTS_DIR} --junitxml=results.xml
                """
            }
        }

        stage('Generate Insights') {
            steps {
                allure includeProperties: false, results: [[path: "${RESULTS_DIR}"]]
                junit 'results.xml'
            }
        }
    }

    post {
        always {
            echo 'Cleaning up workspace...'
            cleanWs()
        }
        failure {
            echo 'Test execution failed. Sending alerts...'
        }
    }
}

Execution and Monitoring

Once the pipeline is saved, trigger a manual build using Build Now to verify the configuration. The Allure Report icon will appear on the project dashboard after the first successful run, providign a detailed breakdown of API responses, execution times, and failure logs. If the build fails, the post block ensures that the workspace remains clean while allowing for automated error reporting to your team's communication channels.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.