Table of Contents
- Prerequisites
- Setting Up Jenkins
- Installing Python and Dependencies on Jenkins
- Creating a Jenkins Job for Python Testing
- Configuring the Jenkins Job
- 5.1 Source Code Management (SCM)
- 5.2 Build Triggers
- 5.3 Build Steps
- 5.4 Post-build Actions
- Running Tests and Analyzing Results
- Advanced Configurations
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites
Before diving in, ensure you have the following:
- A server (local or cloud) to run Jenkins (minimum 2GB RAM, 2CPU cores recommended).
- Basic familiarity with Python, Git, and command-line tools.
- A Python project with tests (using
pytestorunittest). Example structure:my_python_project/ ├── src/ # Source code ├── tests/ # Test files (e.g., test_math_ops.py) ├── requirements.txt # Dependencies (e.g., pytest, requests) └── .git/ # Git repository (hosted on GitHub/GitLab/Bitbucket) - Jenkins installed (see Jenkins Installation Guide).
Setting Up Jenkins
Step 1: Initial Jenkins Setup
After installing Jenkins, access it via http://<your-server-ip>:8080. Unlock Jenkins using the initial admin password (found in /var/lib/jenkins/secrets/initialAdminPassword on Linux).
Step 2: Install Required Plugins
Jenkins relies on plugins for extended functionality. Install these essential plugins for Python CI:
- Git Plugin: For integrating with Git repositories.
- JUnit Plugin: For parsing test results (works with
pytest/unittest). - GitHub Integration Plugin (optional): For GitHub webhooks.
- Pipeline Plugin (optional): For defining CI/CD pipelines as code.
To install plugins:
- Go to Manage Jenkins > Plugins.
- Switch to the Available tab, search for the plugins above, and install them (restart Jenkins if prompted).
Installing Python and Dependencies on Jenkins
Jenkins runs tests on its host server, so we need to install Python and test dependencies there.
Step 1: Install Python
- Linux (Ubuntu/Debian):
sudo apt update && sudo apt install python3 python3-pip python3-venv -y - Windows: Download Python from python.org and check “Add Python to PATH” during installation.
- macOS: Use Homebrew:
brew install python3.
Verify installation:
python3 --version # Should return Python 3.x.x
pip3 --version # Should return pip 20.x.x+
Step 2: Configure Python in Jenkins
Tell Jenkins where to find Python:
- Go to Manage Jenkins > Global Tool Configuration.
- Under Python Installations, click Add Python.
- Name it (e.g.,
Python 3.9), select Install automatically (or point to the local Python path if manually installed). - Save the configuration.
Creating a Jenkins Job for Python Testing
We’ll start with a Freestyle project (simple for beginners) before exploring advanced pipelines.
Step 1: Create a New Job
- From the Jenkins dashboard, click New Item.
- Name it (e.g.,
Python-Test-Pipeline), select Freestyle project, and click OK.
Configuring the Jenkins Job
5.1 Source Code Management (SCM)
Link your Git repository to Jenkins:
- Under Source Code Management, select Git.
- Enter your repository URL (e.g.,
https://github.com/your-username/my_python_project.git). - Add credentials (GitHub username/password or SSH key) if the repo is private.
- Specify the branch to test (e.g.,
*/main).
5.2 Build Triggers
Automatically trigger tests when code is pushed:
- Poll SCM: Check for changes periodically (e.g.,
H/5 * * * *to poll every 5 minutes). - GitHub Hook Trigger for GITScm Polling (recommended): Use GitHub webhooks to trigger builds instantly.
To set up GitHub webhooks:
- In your GitHub repo, go to Settings > Webhooks > Add webhook.
- Payload URL:
http://<jenkins-server-ip>:8080/github-webhook/. - Content type:
application/json. - Secret: (Optional) Add a secret for security (configure in Jenkins under Manage Jenkins > Configure System > GitHub > Add GitHub Server).
5.3 Build Steps
Define commands to run tests. Add a Execute shell (Linux/macOS) or Execute Windows batch command (Windows) build step:
Example Linux/macOS Commands:
# Create a virtual environment to isolate dependencies
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run tests and generate JUnit-style report (required for Jenkins to parse results)
pytest tests/ --junitxml=test-results.xml -v
Example Windows Commands:
python -m venv venv
venv\Scripts\activate.bat
pip install -r requirements.txt
pytest tests/ --junitxml=test-results.xml -v
5.4 Post-build Actions
After tests run, Jenkins can analyze results and notify stakeholders:
-
Publish JUnit test result report:
- Check Publish JUnit test result report.
- Enter
test-results.xml(matches the file generated bypytest). - This displays test trends, failed tests, and pass rates in Jenkins.
-
Email Notification (optional):
- Check E-mail Notification.
- Enter recipient emails (e.g.,
[email protected]). - Configure SMTP in Manage Jenkins > Configure System > E-mail Notification.
Running Tests and Analyzing Results
Trigger the Job Manually
- From the job dashboard, click Build Now.
- Jenkins will checkout your code, run the build steps, and display the build number (e.g., #1).
View Results
- Console Output: Click the build number > Console Output to debug failures (e.g., missing dependencies).
- Test Results: Click Test Result to see:
- Total tests run, passed, failed, skipped.
- Detailed logs for failed tests (e.g.,
AssertionError: 2 + 2 != 5). - Trend charts (Test Result Trend) to track long-term test health.
Advanced Configurations
7.1 Parallel Testing
Speed up tests by running them in parallel (useful for large test suites). Use pytest-xdist to split tests across CPU cores:
-
Install
pytest-xdistinrequirements.txt:pytest>=7.0.0 pytest-xdist>=2.5.0 # For parallel testing -
Update the build step to run:
pytest tests/ --junitxml=test-results.xml -n auto # -n auto uses all CPU cores
7.2 Notifications (Slack)
Send test results to Slack:
- Install the Slack Notification Plugin.
- In Jenkins, go to Manage Jenkins > Configure System > Slack.
- Workspace: Your Slack workspace (e.g.,
mycompany). - Integration Token: Create a Slack bot token (via Slack API).
- Workspace: Your Slack workspace (e.g.,
- In your job, add a Post-build Action > Slack Notification.
- Channel:
#dev-team. - Message:
Build #${BUILD_NUMBER} ${BUILD_STATUS}: ${JOB_NAME}.
- Channel:
7.3 Docker Integration
Run tests in a Docker container for environment consistency (avoids “it works on my machine” issues):
Step 1: Create a Dockerfile in your project:
FROM python:3.9-slim
WORKDIR /app
# Copy dependencies file
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy project files
COPY . .
# Command to run tests
CMD ["pytest", "tests/", "--junitxml=test-results.xml"]
Step 2: Update Jenkins Build Step to Use Docker:
# Build the Docker image
docker build -t python-test-image .
# Run tests in a container and copy results to the host
docker run --name test-container python-test-image
docker cp test-container:/app/test-results.xml .
docker rm test-container
Troubleshooting Common Issues
- Python not found: Ensure Python is in Jenkins’
PATHor specify the full path (e.g.,/usr/bin/python3). - Virtualenv activation fails: Use absolute paths (e.g.,
/var/lib/jenkins/workspace/Python-Test-Pipeline/venv/bin/activate). - Test results not published: Verify
--junitxml=test-results.xmlis included in thepytestcommand and the file path matches in Post-build Actions. - Permission errors: Ensure the Jenkins user (e.g.,
jenkinson Linux) has read access to the project directory and write access to the workspace.
Conclusion
By integrating Jenkins into your Python workflow, you ensure tests run automatically on every code change, catching bugs early and maintaining code quality. This guide covered the basics (setting up Jenkins, configuring jobs) and advanced topics (parallel testing, Docker, notifications). With this pipeline, your team can focus on writing code while Jenkins handles the tedious work of validation.