As APIs become increasingly crucial in modern software development, maintaining comprehensive test coverage is essential. In this post, I'll share a complete solution for running Postman collections using Jenkins, perfect for regression testing and continuous integration.
The Problem
Many organizations maintain extensive API test suites using Postman collections. However, running these tests manually is time-consuming and prone to human error. We needed a solution that would:
- Automate the execution of multiple Postman collections
- Generate detailed HTML reports
- Send email notifications with results
- Support different environments (QA/Staging/Prod)
- Integrate seamlessly with our CI/CD pipeline
The Solution
I've created a Jenkins pipeline that automates the entire process using Newman (Postman's command-line collection runner). Here are the key features:
- Parallel execution of multiple Postman collections
- Customizable HTML reports with detailed test results
- Email notifications with a summary table
- Environment-specific configurations
- Artifact archiving for historical tracking
Prerequisites
- Jenkins server
- NodeJS installed on Jenkins
- Postman collections and environments
- Postman API key
Implementation Details
1. Pipeline Configuration
The pipeline uses Jenkins parameters to make it flexible:
parameters {
text(
name: 'COLLECTIONS_JSON',
defaultValue: '''[
{"name": "Sample_Collection",
"url": "https://api.postman.com/collections/YOUR_COLLECTION_ID?apikey=${POSTMAN_API_KEY}"
}
// ... other collections
]''',
description: 'JSON array of collections to run'
)
choice(
name: 'ENVIRONMENT',
choices: ['QA', 'Prod'],
description: 'Select the environment'
)
}
// ... The complete code is available on GitHub (https://github.com/abu-sithik/jenkins_postman_pipeline).
2. Newman Integration
The pipeline installs Newman and its HTML reporter:
stage('Install Newman') {
steps {
sh 'npm install -g newman'
sh 'npm install -g newman-reporter-htmlextra'
}
}
3. Collection Execution
Each collection runs with environment-specific configurations:
def runCollection(collection, environment_url) {
// ... collection execution logic
// ... The complete code is available on GitHub (https://github.com/abu-sithik/jenkins_postman_pipeline).
}
4. HTML Report Generation
The solution includes a custom HTML report generator:
def generateHtmlReport(reportData) {
// ... HTML generation logic
// ... The complete code is available on GitHub (https://github.com/abu-sithik/jenkins_postman_pipeline).
}
5. Email Notifications
Results are automatically emailed to stakeholders:
def sendEmail(htmlContent, emailConfig) {
emailext(
subject: "${emailConfig.subject}",
body: htmlContent,
to: emailConfig.recipients,
from: emailConfig.sender,
mimeType: 'text/html'
)
}
Setting Up the Pipeline
1. Required Plugins
Install the following Jenkins plugins:
- NodeJS Plugin
- Email Extension Plugin
- Pipeline Plugin
- Credentials Plugin
Manage Jenkins > Manage Plugins > Available > Search and install each plugin
2. NodeJS Configuration
- Navigate to
Manage Jenkins > Tools
- Click "Add NodeJS"
- Configure as follows:
- Name:
NodeJS_22
- Install automatically: Check
- Version: Select latest LTS version
- Name:
- Click Save
3. Credentials Setup
- Navigate to
Manage Jenkins > Manage Credentials
- Click on "Jenkins" under Stores scoped to Jenkins
- Click "Global credentials"
- Click "Add Credentials"
- Configure:
- Kind: Secret text
- Scope: Global
- Secret: Your Postman API key
- ID: POSTMAN_API_KEY
- Description: Postman API Key for Collection Runner
4. Email Configuration
- Navigate to
Manage Jenkins > Configure System
- Find "Extended E-mail Notification"
- Configure:
- SMTP server
- SMTP port
- Credentials if required
- Default recipients
- Default subject
- Default content
Pipeline Configuration
1. Create Pipeline Job
- Click "New Item" on Jenkins dashboard
- Enter name for your pipeline
- Select "Pipeline"
- Click OK
2. Configure Pipeline
- In pipeline configuration:
- Select "Pipeline script" or "Pipeline script from SCM"
- If using SCM:
- Select Git
- Enter repository URL
- Specify branch
- Script Path: Jenkinsfile
3. Environment Configuration
Update the ENVIRONMENT_URLS
variable in Jenkinsfile:
ENVIRONMENT_URLS = [
QA: [id: 'YOUR_QA_ENV_ID', url: ''],
Staging: [id: 'YOUR_STAGING_ENV_ID', url: ''],
Production: [id: 'YOUR_PROD_ENV_ID', url: '']
]
Postman Setup
1. Collection Preparation
- Create/organize your Postman collections
- Ensure all environment variables are properly set
- Add appropriate tests to requests
- Get collection IDs from Postman
2. Environment Setup
- Create environments in Postman for each target (QA, Staging, Production)
- Set appropriate variables
- Get environment IDs from Postman
First Run
- Open pipeline in Jenkins
- Click "Build with Parameters"
- Enter test configuration:
[
{
"name": "Test_Collection",
"url": "https://api.postman.com/collections/YOUR_COLLECTION_ID?apikey=${POSTMAN_API_KEY}"
}
]
- Select environment
- Enter email recipients
- Click Build
Conclusion
This solution has significantly improved our API testing process by:
- Reducing manual effort
- Providing consistent test execution
- Generating comprehensive reports
- Enabling early detection of API issues
The complete script is available on GitHub.
Top comments (0)