Debug School

rakesh kumar
rakesh kumar

Posted on

How to integrate unit testing or selenium testing into a CI/CD pipeline with Jenkins

Unit Testing with Python and pytest:
Assuming you have a Python project with pytest for unit testing:

Write Unit Tests:

Create unit tests using the pytest framework. For example, create a file named test_myapp.py:

# test_myapp.py
def test_addition():
    assert 1 + 1 == 2

def test_subtraction():
    assert 3 - 1 == 2
Enter fullscreen mode Exit fullscreen mode

Integrate with Jenkins:

Create a Jenkins pipeline script (usually in a Jenkinsfile) that includes the following steps:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Unit Test') {
            steps {
                script {
                    sh 'pip install -r requirements.txt'  // Install project dependencies
                    sh 'pytest'  // Run unit tests
                }
            }
        }

        // Additional stages for build, deploy, etc.
    }

    post {
        always {
            // Cleanup or additional steps
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Ensure that Jenkins is configured to trigger the pipeline when changes are pushed to your repository.

Selenium Testing:
Assuming you have a Selenium project with Python and pytest:

Write Selenium Tests:

Create Selenium tests using the pytest framework and a WebDriver of your choice. For example, create a file named test_selenium.py:

# test_selenium.py
from selenium import webdriver

def test_title():
    driver = webdriver.Chrome()
    driver.get('https://example.com')
    assert 'Example Domain' in driver.title
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Ensure you have the necessary Selenium and WebDriver dependencies installed.

Integrate with Jenkins:

Extend your Jenkins pipeline script to include Selenium testing:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Unit Test') {
            steps {
                script {
                    sh 'pip install -r requirements.txt'  // Install project dependencies
                    sh 'pytest tests' // Run unit tests
                }
            }
        }

        stage('Selenium Test') {
            steps {
                script {
                    sh 'pip install -r requirements-selenium.txt'  // Install Selenium and WebDriver dependencies
                    sh 'pytest --driver=chrome test_selenium.py'  // Run Selenium tests
                }
            }
        }

        // Additional stages for build, deploy, etc.
    }

    post {
        always {
            // Cleanup or additional steps
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Ensure that you have the appropriate WebDriver installed and configured on the Jenkins agent where the pipeline runs.

Notes:
In these examples, the pytest command is used for simplicity. Make sure to adapt the testing commands and configurations based on your specific project structure and testing frameworks.
Adjust the WebDriver and browser configurations according to your Selenium testing needs.
Properly handle dependencies, virtual environments, and cleanup in your pipeline.
The Jenkins pipeline syntax may vary based on your Jenkins setup and pipeline requirements.

Explanation

/your_project
   /tests
      test_myapp.py
   requirements.txt
   ...
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)