DEV Community

Cover image for Running Postman Collections in Jenkins
Abubakkar Sithik
Abubakkar Sithik

Posted on

Running Postman Collections in Jenkins

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).
Enter fullscreen mode Exit fullscreen mode

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'
    }
}
Enter fullscreen mode Exit fullscreen mode

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).
}
Enter fullscreen mode Exit fullscreen mode

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).
}
Enter fullscreen mode Exit fullscreen mode

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'
    )
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. NodeJS Configuration

  1. Navigate to Manage Jenkins > Tools
  2. Click "Add NodeJS"
  3. Configure as follows:
    • Name: NodeJS_22
    • Install automatically: Check
    • Version: Select latest LTS version
  4. Click Save

3. Credentials Setup

  1. Navigate to Manage Jenkins > Manage Credentials
  2. Click on "Jenkins" under Stores scoped to Jenkins
  3. Click "Global credentials"
  4. Click "Add Credentials"
  5. 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

  1. Navigate to Manage Jenkins > Configure System
  2. Find "Extended E-mail Notification"
  3. Configure:
    • SMTP server
    • SMTP port
    • Credentials if required
    • Default recipients
    • Default subject
    • Default content

Pipeline Configuration

1. Create Pipeline Job

  1. Click "New Item" on Jenkins dashboard
  2. Enter name for your pipeline
  3. Select "Pipeline"
  4. Click OK

2. Configure Pipeline

  1. 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: '']
]
Enter fullscreen mode Exit fullscreen mode

Postman Setup

1. Collection Preparation

  1. Create/organize your Postman collections
  2. Ensure all environment variables are properly set
  3. Add appropriate tests to requests
  4. Get collection IDs from Postman

2. Environment Setup

  1. Create environments in Postman for each target (QA, Staging, Production)
  2. Set appropriate variables
  3. Get environment IDs from Postman

First Run

  1. Open pipeline in Jenkins
  2. Click "Build with Parameters"
  3. Enter test configuration:
   [
       {
           "name": "Test_Collection",
           "url": "https://api.postman.com/collections/YOUR_COLLECTION_ID?apikey=${POSTMAN_API_KEY}"
       }
   ]
Enter fullscreen mode Exit fullscreen mode
  1. Select environment
  2. Enter email recipients
  3. 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)