Skip to content

Postman脚本工程化及CICD

目录

  • 环境搭建
  • 认识postman
  • 接口测试场景在postman中的应用
  • postman脚本工程化
  • 持续集成

环境搭建

软件安装list

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装好以后可以使用brew安装你想安装的软件,如: eg1: brew install wget eg2: brew install google-chrome

brew install postman
brew install jenkins
brew install node
newman -h
  • 安装newman报告加强版
npm install -g newman-reporter-htmlextra

启动jenkins,保存密码

➜  ~ jenkins
Running from: /usr/local/Cellar/jenkins/2.316/libexec/jenkins.war
webroot: $user.home/.jenkins
2021-12-15 03:37:39.326+0000 [id=40] INFO jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

28411df2f2954b418446981475ed3979

This may also be found at: /Users/panxueyan1/.jenkins/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
2021-12-15 03:38:04.404+0000 [id=23] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running

浏览器打开地址:http://127.0.0.1:8080/, 输入密码安装插件

入门

图片

认识postman

  1. 认识postman中的变量
  • 变量范围:

图片

  • Global variables

图片

  • Collection variables
var hasGoodsPoolName = `商品池新建0001`;
pm.collectionVariables.set("hasGoodsPoolName", hasGoodsPoolName);
  • Environment variables

图片

图片

  1. 场景3:参数化(测试数据,环境变量)
  2. 场景4:参数关联(接口一的reponse参数是接口二的入参)
  3. 场景5:前置条件及结果校验

postman脚本工程化

  1. 工程化目录如下
➜  postmanProject ls
env      ppt.md   reports  scripts  testdata
➜  postmanProject tree
.
├── env
│   ├── jubu.postman_environment.json
│   └── workspace.postman_globals.json
├── ppt.md
├── reports
│   ├── jshop_interface_test-2021-12-15-07-16-30-548-0.html
│   ├── jshop_interface_test-2021-12-15-07-30-21-608-0.html
│   └── jshop_interface_test-2021-12-15-07-31-29-201-0.html
├── scripts
│   └── jshop_interface_test.postman_collection.json
└── testdata

4 directories, 7 files

图片

  1. 命令行运行:
newman run scripts/jshop_interface_test.postman_collection.json -e env/jubu.postman_environment.json -g env/workspace.postman_globals.json -r cli,htmlextra --reporter-htmlextra-export ./reports

图片

生成的html报告如下:图片

推送coding仓库,jenkins自动构建,发送测试报告

  • 新建coding仓库

图片

  • 在mac端生成sshkey公钥
$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:local
  • coding仓库配置公钥 把id_rsa.pub公钥内容配置到coding设置ssh上

图片

  • 本地代码添加git管理并添加远程仓库提交代码
git inti 
git remote add origin 代码仓库
git pull origin master
git add .
git commit -m "add interface scripts"
git push origin master

图片

  • 创建jenkins pipline流水线运行
pipeline {
    agent any

    stages {
        
        stage('active env') {
            steps {
                sh "#!/bin/bash -il && source /etc/profile && source ~/.bash_profile"
            }
        }
        
        stage('delete existed projects') {
            steps {
                sh "rm -rf ${WORKSPACE}/autoplatProject"
            }
        }
        
        stage('clone remote branch') {
            steps {
                sh "git clone https://xxx/panxueyan1/autoplatProject.git"
            }
        }
        
        stage('change dir') {
            steps {
                sh "cd ${WORKSPACE}/autoplatProject"
            }
        }
        
        
        stage('excute newman cli') {
            steps {
                sh "newman run ${WORKSPACE}/autoplatProject/scripts/jshop_interface_test.postman_collection.json -e ${WORKSPACE}/autoplatProject/env/jubu.postman_environment.json -g ${WORKSPACE}/autoplatProject/env/workspace.postman_globals.json -r cli,htmlextra --reporter-htmlextra-export ${WORKSPACE}/autoplatProject/reports/test.html"
            }
        }
        
  
        
        stage('send reports via email') {
            steps {
                sh "python ${WORKSPACE}/autoplatProject/send_email.py"
            }
        }
    }
}

运行结果如下:

图片