We may not have the course you’re looking for. If you enquire or give us a call on +32 35001305 and speak to our training experts, we may still be able to help with your training requirements.
Training Outcomes Within Your Budget!
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
Jenkins software testing automation tool was a creation by Kohsuke Kawaguchi to simplify the software testing process. It is accessible in multiple different languages with the right prerequisites. One of many such languages is Python; you can use Jenkins Python client to execute Python scripts in Jenkins.
According to LinkedIn, Python was used for 16% of software testing globally in 2023. If you also wish to start the software testing process in Python, this blog is just for you. In this article, you will learn about Jenkins Python and how Python can be integrated into Jenkins for testing and reporting. Read further to learn more!
Table of Contents
1) Using Python in Jenkins
a) Installing Jenkins
b) Installing Python libraries
2) Jenkins Python Script
3) Python program in Jenkins
4) Conclusion
Using Python in Jenkins
Jenkins has changed how developers and testers view the integration of new code blocks in base codes. Jenkins was responsible for simplifying the testing process and replacing “Nightly Builds” with Continuous Integration (CI). As time has passed, Jenkins has gained recognition for added benefits that came with simplifying the testing process.
Jenkins is one of the most plugin-friendly tools in Software testing, allowing you to extend its capabilities significantly. Since Jenkins was developed initially in Java, using it in different languages would be extremely difficult without a plugin. This allows you to use Jenkins in a user-friendly language known and praised for its readability.
Python is not a compiled language, which simplifies integrating it into Jenkins. Additionally, python’s libraries allow you to integrate multiple tools that simplify the testing process. Let's see the prerequisites for Using Jenkins with Python programming language.
Installing Jenkins
Installing the Jenkins automation tool in your system is your first step to achieving Python integration. For those who do not have prior experience with Jenkins, here are helpful steps to aid you in installing Jenkins.
Step 1: Download the jenkins.war file, which is available on the official Jenkins website.
Step 2: Opens the file directory of jenkins.rar file in your system's command prompt.
Step 3: Run the command java -jar jenkins.war in your command prompt.
Step 4: If you successfully run the command, multiple files will load. You'll find a password being generated amongst the files, copy this password; it will help you later.
Step 5: Type localhost:8080 in your browser. This will lead you to a Jenkins Unlock page successfully.
Step 6: The Unlock page will demand a security token from you on this point; paste the previously copied password to finish the process.
Installing Python libraries
Here is a list of tools that can be integrated into Python, resulting in a smoother experience. These libraries can be installed using the pip installation method within Python. For example, the “pytlint” library can be used for report generation, which can be integrated directly into Jenkins.
Libraries in Python help you in generating quality code test reports. Similarly, “Nose2” can be used for unit testing in a project and is one of the popular tests running libraries. You can additionally use “pytest” for generating test reports in XML formats.
Jenkins Python Script
Python 3 edition allows you to integrate a client library to work with Jenkins Application Package Interface. The library makes the query and filtering process of item attributes relatively easy. Additionally, these are based on api/json and are Object Oriented, making them efficient.
Currently, the Jenkins API for python, labelled “jenkinsapi 0.3.13” allows you to work with the Continuous-Integration process on Jenkins Server. Libraries like these are necessary, as Jenkins was originally developed in Java. These libraries act as an interface, making it convenient for you to use Jenkins with Python.
Additionally, you can use the “subprocess” module to run Jenkins with Python scripts. Here is an example of a program that runs the Jenkins automation tool using Python script. This script will use Python in the terminal, which will print a message when Jenkins starts. This process uses the termination command “jenkins_process.terminate()” to terminate the code at the end.
Write a program to demonstrate running Jenkins with Python Script |
import subprocess jenkins_command = 'java -jar jenkins.war' # Replace with the command to run Jenkins on your system jenkins_process = subprocess.Popen(jenkins_command, shell=True) # Wait for Jenkins to start up jenkins_process.wait() # Do something else while Jenkins is running print('Jenkins is running. Do something else here...') # Stop Jenkins jenkins_process.terminate() |
Python installation in Jenkins
Python has an inbuilt library known as pip, which can be used to install readily available plugins and APIs. You can use the installation command “pip install jenkinsapi” or “easy_install jenkinsapi” to integrate it successfully. Alternatively, you can use “apt-get install python-jenkinsapi" for the Operating System package installer.
You can create a new job or trigger a build using Python codes using Jenkins in Python Script. You can alternatively use different commands like “jenkins.exe” for exe files. If you wish to run the program in a shell, you might need to use “shell=True” argument in your Python program.
Python program in Jenkins
Here is an example of a Python program using Jenkins API to use the automation tool. The Python program uses a Jenkins Uniform Resource Locator (URL) to create a Jenkins object. The program will then use various methods to get the necessary information. This program can be modified based on your needs and can retrieve plugins, nodes and builds.
Write a program in Python for object creations in Jenkins |
from jenkinsapi.jenkins import Jenkins jenkins_url = 'http://localhost:8080' # Replace with your Jenkins URL jenkins_username = 'your_username' # Replace with your Jenkins username jenkins_password = 'your_password' # Replace with your Jenkins password job_name = 'my_job' # Replace with the name of your Jenkins job jenkins = Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) job = jenkins[job_name] print('Job Name: {}'.format(job.name)) print('Job Description: {}'.format(job.get_description())) print('Job URL: {}'.format(job.url)) print('Last Build Number: {}'.format(job.get_last_buildnumber())) print('Last Successful Build Number: {}'.format(job.get_last_successful_buildnumber())) |
Learn more with our Python Scripting Training course!
Conclusion
As we conclude this blog, we hope you were able to understand the concept of using the Jenkins automation tool with Python. Hopefully, now you better understand Jenkins Python Client libraries and their installation methods. Thank you for reading.
Build a stronger knowledge of Python with our Python Programming Training!
Frequently Asked Questions
Upcoming Programming & DevOps Resources Batches & Dates
Date
Thu 30th Jan 2025
Thu 27th Mar 2025
Thu 29th May 2025
Thu 24th Jul 2025
Thu 25th Sep 2025
Thu 27th Nov 2025