postman+newman+Jenkins实现接口自动化回归测试

1. postman

postman上创建项目,编写测试脚本

《postman+newman+Jenkins实现接口自动化回归测试》 postman.png

导出脚本文件及环境变量文件

《postman+newman+Jenkins实现接口自动化回归测试》 postman导出json脚本.png
《postman+newman+Jenkins实现接口自动化回归测试》 postman导出环境变量.png

2. newman

安装
  1. 需要先安装nodejs,并配置好环境变量

  2. 在nodejs命令行安装newman,命令行输入如下命令:
    npm install -g newman

执行命令
  • run 脚本路径
  • -e 环境变量路径
  • -g 全局变量路径
  • –reporter-html-export 报告路径及名称
  • -n 设置运行集合的次数

eg:

newman run C:\Users\admin\API-testing\cmt_operation\CMT_运营管理_流程脚本.postman_collection.json --environment C:\Users\admin\API-testing\cmt_operation\test2_CMT.postman_environment.json --reporters cli,html,json,junit --reporter-json-export C:\Users\admin\API-testing\jsonOut.json --reporter-junit-export C:\Users\admin\API-testing\xmlOut.xml --reporter-html-export C:\Users\admin\API-testing\htmlOut.html

《postman+newman+Jenkins实现接口自动化回归测试》 newman执行结果.png

3. Jenkins

安装
  1. 需要java环境的安装配置,这里不再说明

  2. 安装tomcat,(http://tomcat.apache.org/),安装完目录结构如下

《postman+newman+Jenkins实现接口自动化回归测试》 tomcat.png

  1. 安装Jenkins,(https://jenkins.io/),直接将其安装到Tomcat的 webapps 目录下
配置Jenkins

安装完成后,jenkins服务启动默认的端口是8080,会和tomcat的默认端口冲突,可以在jenkins.xml中,修改端口号,并在window任务管理器中重启jenkins服务。

《postman+newman+Jenkins实现接口自动化回归测试》 修改端口号.png

使用新的URL:http://localhost:8888/ 通过初始密码登录。

《postman+newman+Jenkins实现接口自动化回归测试》 jenkins初始界面.png

根据提示,到对应文件查看密码。将密码填写到输入框中。后续根据提示配置即可。

新建任务

《postman+newman+Jenkins实现接口自动化回归测试》 新建任务.png
《postman+newman+Jenkins实现接口自动化回归测试》 构建触发器.png
《postman+newman+Jenkins实现接口自动化回归测试》 构建命令.png

至此,该项目的自动化构建已完成。

踩坑记录:
  1. 执行构建任务,报错

《postman+newman+Jenkins实现接口自动化回归测试》 缺少环境变量.png

  • 原因:Jenkins缺少环境变量
  • 解决:配置环境变量,同时还需要添加浏览器驱动文件所在目录

    《postman+newman+Jenkins实现接口自动化回归测试》 环境变量.png

  1. 执行构建任务,能正确执行,但是控制台输出乱码

《postman+newman+Jenkins实现接口自动化回归测试》 控制台乱码.png

  • 原因:编码格式问题
  • 解决: 在Jenkins安装目录下找到jenkins.xml文件,找到<arguments> ……</arguments>,在中间添加-Dfile.encoding=utf-8
  1. 改完jenkins编码后,执行报错

《postman+newman+Jenkins实现接口自动化回归测试》 报错信息.png

  • 原因:我是windows系统,编码格式默认GBK,与Jenkins utf-8冲突,解码后找不到对应文件
  • 解决:文件名不要含中文!!!
    或者把jenkins部在虚拟机上。不是windows系统不会有编码问题。
    原文作者:JoyceChen_
    原文地址: https://www.jianshu.com/p/b1771113162d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞