Skip to content

Jenkins CI

Jenkins is a popular automation tool used to run tasks such as building applications and executing tests whenever code changes. It is most commonly used for continuous integration (CI), a development practice where tests are run automatically each time new code is added to a shared repository. The goal of CI is to catch bugs early by verifying that new changes do not break existing functionality.

pytest integrates naturally with Jenkins because it can be executed directly from the command line. In a Jenkins job or pipeline, running pytest is typically as simple as invoking it as a build step. Here is an example Jenkins pipeline:

groovy
pipeline {
    agent any

    stages {
        stage('Install dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }

        stage('Run tests') {
            steps {
                sh 'pytest'
            }
        }
    }
}

If all tests pass, pytest exits successfully and the Jenkins job is marked as successful. If any test fails, pytest exits with a non-zero status code and Jenkins reports a failed build, providing immediate feedback on code changes.

For improved reporting, pytest can generate JUnit-style XML output that Jenkins can display in its web interface:

shell
pytest --junitxml=test-results.xml

In many projects, Jenkins runs pytest through tools like tox, allowing the same test commands to be used both locally and in automated builds.