如何为Postman测试设置Jenkins流水线

3

Postman是一款流行的API测试工具,您可以使用Postman编写大量单元测试,并将其作为构建过程的一部分执行以进行单元测试。以下内容涵盖了Postman测试与Jenkins集成。

为了实现这一点,您应该:

  1. 将Postman测试导出为一个集合。
  2. 在测试执行时使API可用(使用Docker或创建单独的构建管道)。
2个回答

6

Node模块newman可以用来执行Postman集合。请参考以下Package.json文件。我们将使用newman执行位于unit_tests文件夹中的Postman集合,并定义了newman依赖项。

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"
  }
}

以下是Jenkinsfile的内容。我们使用NPM安装依赖项并执行测试。
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

您可以避免在机器(从属/主)上安装newman并使用docker

示例管道脚本:

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

更多有关Docker和Newman的信息,请在此处查看。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接