How to setup a Jenkins pipeline for postman tests

2019-08-03 07:16发布

问题:

Postman is used as a popular testing tool for API testing, you can write bunch of unit tests using Postman and execute them as a part of your build process to perform unit testing. The following covers the Jenkins integration of Postman tests.

In order to do that you should have

  1. Exported Postman tests as a collection
  2. Make the APIs available at run time when the tests are performed. (Using docker or by creating a separate build pipeline.)

回答1:

Node module newman can be used to execute Postman collections. Refer the following Package.json file. Here we are executing the postman collection inside the unit_tests folder using newman, also newman dependency is defined.

package.json

{
  "name": "postman-newman-jenkins",
  "version": "1.0.0",
  "description": "My Test Project",
  "directories": {
    "tests": "tests"
  },
  "scripts": {
    "newman-tests": "newman run unit_tests/my-collection.postman_collection.json --reporters cli,junit --reporter-junit-export newman.xml --insecure"
  },
  "author": "Test Author",
  "dependencies": {
    "newman": "^3.5.2"
  }
}

The following is the content of the Jenkinsfile. We are using NPM to install the dependencies and execute tests.

Jenkinsfile

pipeline {
    agent { label 'LinuxSlave' }
    stages {
        stage ('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Test'){
            steps {
                sh 'npm install'
                sh 'npm run newman-tests'
                junit 'newman.xml'
            }
        }
    }
}


回答2:

you can avoid installing newman on the machine (slave/ master) and use docker

example pipeline script:

pipeline {
 agent any stages {
  stage('Test') {
   steps {
    sh 'docker run -t postman/newman_ubuntu1404 run https://www.getpostman.com/collections/8a0c9bc08f062d12dcda'
   }
  }
 }
}

more info on docker & newman here