Project: Automated Code Testing with Jenkins and Git Webhooks
Objective
To automate unit testing using Jenkins, where every code push to a Git repository triggers a pipeline that runs unit tests (e.g., JUnit for Java or PyTest for Python). This ensures code quality before deployment.
Project Components
- Version Control: GitHub/GitLab/Bitbucket
- CI/CD Tool: Jenkins
- Programming Language & Test Framework:
- Java with JUnit
- Python with PyTest
- Build Tool:
- Maven for Java
- pip/virtualenv for Python
- Webhooks: GitHub Webhook (to trigger Jenkins build)
- Infrastructure: Jenkins Server (Self-hosted or AWS EC2)
- Artifacts Storage: (Optional) Nexus/S3 for storing test reports
- Notifications: Slack/Email for test results
Implementation Steps
1. Set Up Jenkins
- Install Jenkins on an EC2 instance or local server.
- Install required plugins:
- Git Plugin
- Pipeline Plugin
- JUnit Plugin
- Email Extension Plugin
- Slack Notification Plugin (optional)
2. Configure Git Webhook
- Go to your GitHub repository → Settings → Webhooks → Add webhook
- Enter the Jenkins URL:
http://<JENKINS_SERVER>:8080/github-webhook/
Set trigger to "Push events"
Jenkins will receive a payload whenever code is pushed.
3. Create a Jenkins Pipeline
Pipeline Structure
- Step 1: Clone repository from GitHub
- Step 2: Set up dependencies
- Step 3: Run unit tests (JUnit/PyTest)
- Step 4: Publish test reports
- Step 5: Notify team (Slack/Email)
Example Jenkinsfile
For Java (JUnit with Maven)
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/your-repo.git'
}
}
stage('Build Project') {
steps {
sh 'mvn clean install'
}
}
stage('Run Unit Tests') {
steps {
sh 'mvn test'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
}
post {
success {
echo "Tests passed!"
}
failure {
echo "Tests failed!"
}
}
}
For Python (PyTest with Virtualenv)
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/your-repo.git'
}
}
stage('Setup Environment') {
steps {
sh 'python3 -m venv venv && source venv/bin/activate'
sh 'pip install -r requirements.txt'
}
}
stage('Run Unit Tests') {
steps {
sh 'pytest --junitxml=pytest_results.xml'
}
post {
always {
junit '**/pytest_results.xml'
}
}
}
}
post {
success {
echo "Tests passed!"
}
failure {
echo "Tests failed!"
}
}
}
4. Configure Notifications
Slack Notification (Optional)
- Install Slack Notification Plugin in Jenkins.
- Configure it in Manage Jenkins → System Configuration.
- Add this to your
Jenkinsfile
:
post {
success {
slackSend channel: '#ci-cd', message: "Build Passed: ${env.BUILD_URL}"
}
failure {
slackSend channel: '#ci-cd', message: "Build Failed: ${env.BUILD_URL}"
}
}
5. Run and Validate
- Push code changes to GitHub.
- Git webhook triggers Jenkins.
- Jenkins runs unit tests.
- View test reports in Jenkins.
- Receive Slack/Email notification.
Expected Outcome
- Every push triggers an automated test run.
- Developers get immediate feedback on test results.
- CI/CD pipeline enforces quality checks before deployment.
Next Steps
- Integrate SonarQube for code quality analysis.
- Add functional tests using Selenium or Cypress.
- Deploy passing builds to staging environments.
This setup ensures a robust CI/CD pipeline with automated testing, improving software quality and development speed.