mirror of
https://github.com/octocat/Hello-World.git
synced 2026-06-04 14:17:09 +00:00
6.9 KiB
6.9 KiB
Payment Test Suite - Jenkins Setup Guide
Project Structure
Hello-World/
├── src/
│ ├── __init__.py
│ └── payment_service.py # Dummy payment service
├── tests/
│ ├── __init__.py
│ └── test_payment.py # Payment test cases (21 tests)
├── Jenkinsfile # Jenkins pipeline configuration
├── requirements.txt # Python dependencies
├── pytest.ini # Pytest configuration
├── .gitignore # Git ignore patterns
├── SETUP_GUIDE.md # This file
└── README # Original readme
Test Cases Included
The test suite includes 21 comprehensive test cases:
Positive Tests
- ✅ Successful payment processing
- ✅ Multiple payments handling
- ✅ Transaction retrieval
- ✅ Successful refund processing
Negative Tests
- ❌ Invalid amount (zero)
- ❌ Invalid amount (negative)
- ❌ Invalid card number (too short)
- ❌ Invalid card number (empty)
- ❌ Invalid CVV (too short)
- ❌ Invalid CVV (empty)
- ❌ Refund non-existent transaction
- ❌ Duplicate refund
Edge Cases
- 🔍 Large amount payment
- 🔍 Small amount payment
- 🔍 Get all transactions (empty)
- 🔍 Get non-existent transaction
- 🔍 Card number masking
Prerequisites
Local Setup
- Python 3.9+ installed
- Git installed
- pip package manager
Jenkins Setup
- Jenkins server installed and running
- Python plugin for Jenkins
- HTML Publisher plugin for Jenkins
- JUnit plugin for Jenkins (usually pre-installed)
Step-by-Step Procedure
Part 1: Local Testing (Optional but Recommended)
-
Clone/Navigate to your repository:
cd d:\Hello-World -
Install dependencies:
pip install -r requirements.txt -
Run tests locally:
# Run all tests pytest tests/ -v # Run with coverage pytest tests/ --cov=src --cov-report=html # Run specific test pytest tests/test_payment.py::TestPaymentService::test_successful_payment -v -
View coverage report:
# Open htmlcov/index.html in your browser start htmlcov/index.html
Part 2: Jenkins Setup
Option A: Jenkins Pipeline (Recommended)
-
Install Required Jenkins Plugins:
- Go to Jenkins → Manage Jenkins → Manage Plugins
- Install:
- Pipeline
- Git plugin
- HTML Publisher plugin
- JUnit plugin
-
Create New Jenkins Job:
- Click "New Item"
- Enter job name:
Payment-Test-Suite - Select "Pipeline"
- Click OK
-
Configure Pipeline:
- Scroll to "Pipeline" section
- Definition: Select "Pipeline script from SCM"
- SCM: Select "Git"
- Repository URL: Enter your Git repository URL
- Branch:
*/main(or your default branch) - Script Path:
Jenkinsfile - Click "Save"
-
Run the Pipeline:
- Click "Build Now"
- Monitor the build in the console output
Option B: Freestyle Project
-
Create New Jenkins Job:
- Click "New Item"
- Enter job name:
Payment-Test-Suite-Freestyle - Select "Freestyle project"
- Click OK
-
Source Code Management:
- Select "Git"
- Repository URL: Enter your repository URL
- Branch:
*/main
-
Build Steps:
- Add build step → Execute Windows batch command
python -m pip install --upgrade pip pip install -r requirements.txt pytest tests/ -v --junitxml=test-results.xml --html=test-report.html --self-contained-html pytest tests/ --cov=src --cov-report=html --cov-report=xml -
Post-build Actions:
- Add "Publish JUnit test result report"
- Test report XMLs:
test-results.xml
- Test report XMLs:
- Add "Publish HTML reports"
- HTML directory to archive:
htmlcov - Index page:
index.html - Report title:
Coverage Report
- HTML directory to archive:
- Add "Publish JUnit test result report"
-
Save and Build:
- Click "Save"
- Click "Build Now"
Part 3: GitHub/Git Integration
-
Push your code to Git:
git add . git commit -m "Add payment test suite and Jenkins pipeline" git push origin main -
Configure Webhook (Optional - for automatic builds):
- In GitHub: Settings → Webhooks → Add webhook
- Payload URL:
http://your-jenkins-url/github-webhook/ - Content type:
application/json - Select: "Just the push event"
- Active: ✓
-
In Jenkins Job Configuration:
- Build Triggers → Check "GitHub hook trigger for GITScm polling"
Part 4: Viewing Results
After a successful build, you can view:
-
Test Results:
- Click on build number → Test Results
- Shows all 21 test cases with pass/fail status
-
Coverage Report:
- Click on build number → Coverage Report
- Shows code coverage percentage
-
HTML Test Report:
- Click on build number → Test Report
- Detailed HTML report with test execution details
-
Console Output:
- Click on build number → Console Output
- View complete build log
Jenkins Pipeline Stages
The Jenkinsfile includes these stages:
- Checkout - Pulls code from repository
- Setup Python Environment - Verifies Python installation
- Install Dependencies - Installs required packages
- Run Tests - Executes all test cases
- Code Coverage - Generates coverage report
- Publish Results - Publishes test and coverage reports
Troubleshooting
Common Issues
-
Python not found:
- Add Python to system PATH
- In Jenkins: Manage Jenkins → Global Tool Configuration → Add Python
-
Module not found:
- Ensure
requirements.txtis installed - Check virtual environment activation
- Ensure
-
Tests not discovered:
- Verify pytest.ini configuration
- Check test file naming (test_*.py)
-
HTML reports not showing:
- Install HTML Publisher plugin
- Configure Content Security Policy:
Manage Jenkins → Script Console → Run: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
Running Specific Test Categories
# Run only successful payment tests
pytest tests/test_payment.py -k "successful" -v
# Run only negative tests
pytest tests/test_payment.py -k "invalid" -v
# Run with markers (if configured)
pytest tests/ -m "unit" -v
CI/CD Best Practices
- Run tests on every commit
- Maintain >80% code coverage
- Review failed tests immediately
- Keep test execution time < 5 minutes
- Archive test reports for compliance
Next Steps
- Add more test cases for edge scenarios
- Integrate with Slack/Email for notifications
- Add performance testing
- Implement test data management
- Add security scanning stages
Support
For issues or questions:
- Check Jenkins console output
- Review test logs in test-report.html
- Verify Python and dependency versions
Last Updated: May 2026