0
0
PyTesttesting~5 mins

Running PyTest in Jenkins

Choose your learning style9 modes available
Introduction

Running PyTest in Jenkins helps automate testing your Python code every time you make changes. This ensures your code works well and catches errors early.

You want to check your Python tests automatically after each code update.
You need to run tests on a remote server without manual intervention.
You want to see test results and reports in one place.
You want to prevent broken code from being added to your project.
You want to integrate testing into your software delivery process.
Syntax
PyTest
pipeline {
    agent any
    stages {
        stage('Run PyTest') {
            steps {
                sh 'pytest --junitxml=results.xml'
            }
        }
        stage('Publish Test Results') {
            steps {
                junit 'results.xml'
            }
        }
    }
}

This is a Jenkins Pipeline script written in Groovy.

The sh step runs the PyTest command in the shell.

Examples
Runs PyTest with default settings in a Jenkins shell step.
PyTest
sh 'pytest'
Runs PyTest and saves test results in XML format for Jenkins to read.
PyTest
sh 'pytest --junitxml=results.xml'
Publishes the test results in Jenkins using the XML file generated by PyTest.
PyTest
junit 'results.xml'
Sample Program

This Jenkins Pipeline runs PyTest to execute tests and then publishes the test results in Jenkins.

PyTest
pipeline {
    agent any
    stages {
        stage('Run PyTest') {
            steps {
                sh 'pytest --junitxml=results.xml'
            }
        }
        stage('Publish Test Results') {
            steps {
                junit 'results.xml'
            }
        }
    }
}
OutputSuccess
Important Notes

Make sure Python and PyTest are installed on the Jenkins agent machine.

The --junitxml option creates a file Jenkins can read to show test results.

Use the Jenkins junit step to display test results and mark build status.

Summary

Running PyTest in Jenkins automates your Python testing process.

Use a Jenkins Pipeline with sh 'pytest --junitxml=results.xml' to run tests.

Publish results with the junit step to see test outcomes in Jenkins.