postman—使用newman来执行postman脚本

我们知道postman是基于javascript语言编写的,而导出的json格式的postman脚本也无法直接在服务器运行,它需要在newman中执行(可以把newman看做postman脚本的运行环境)

所以要实现在windows的cmd窗口或者linux系统中直接以命令的方式执行脚本,我们需要安装node.js,然后再在此基础上安装newman

一.windows下安装
1.安装node.js
到官网下载最新的windows版node.js,直接安装即可(不用自己去配置环境变量,安装完成后会自动配好)
安装完成后,打开cmd窗口,输入 node -v,如下出现版本号表示安装成功
2.安装newman
可以通过npm来安装newman,npm 是 JavaScript 的包管理工具,并且是 Node.js 平台的默认包管理工具。通过 npm 可以安装、共享、分发代码,管理项目依赖关系。
一般安装好node.js后会默认安装好npm的,直接使用即可
打开cmd窗口,执行以下命令
npm -g install newman(如果npm装不成功,可以先安装cnpm,然后使用cnpm命令安装,cnpm -g install newman)
安装完成后,输入newman -v,如下出现版本号表示安装成功
3.如果想生成html格式的测试报告,还需要安装 newman-reporter-html
npm install -g newman-reporter-html(cnpm install newman-reporter-html)
 
newman的常用命令
使用newman run 来执行脚本,先看下有哪些可选参数
<collection>是指单个请求或者从postman导出的集合文件(也就是json格式的脚本)
options是一些组合参数,介绍下我用到的几个参数
(1) -e 指定环境变量,把在postman中设置的环境变量导出,然后再把路径填写到这里即可
(2) -g 指定全局变量,把在postman中设置的全局变量导出,然后再把路径填写到这里即可
(3) -n 指定迭代次数,即运行n次脚本
(4) --timeout-request 指定请求的超时时间
(5) -r 指定运行报告的格式,可以为json格式、html格式,默认为cli格式,即在命令行展示运行结果

  实例1:没有环境变量,单纯的执行json脚本:

C:\Users\zy>newman run test.postman_collection.json

test.postman_collection.json为导出的json脚本

执行结果:

  实例2:把接口测试脚本和环境变量脚本导出放到一个目录中,在cmd窗口中切换到该目录,执行如下命令

C:\Users\zy>newman run testn.postman_collection.json -n 2 -e Backupn.postman_dump.json

testn.postman_collection.json 接口测试脚本

Backupn.postman_dump.json 环境变量脚本文件

-n 2表示迭代2次

执行过程如下

执行完成后,会出现一个类似报表的东西,显示整体运行结果

  实例3:只生成html报告:

C:\Users\zy>newman run  test.postman_collection.json -r html --reporter-html-export outputfile.html

  实例4:

C:\Users\zy>newman run testn.postman_collection.json -n 2 -e Backupn.postman_dump.json --reporters cli,json,html --reporter-json-export report-json.json --reporter-html-export report-html.html

--reporters cli,json,html --reporter-json-export report-json.json --reporter-html-export report-html.html   表示生成json和html格式的报告

html格式的报告长下面这个样子:
补充:-d,----iteration-data 指定用于迭代的数据源文件路径 
d:\搜索测试\搜索测试.csv   指参数化数据文件
newman run d:\搜索测试\搜索测试.postman_collection.json -e d:\搜索测试\测试项目.postman_environment.json -d d:\搜索测试\搜索测试.csv
posted @ 2019-11-18 20:40  飞鸟与新月  阅读(864)  评论(0编辑  收藏  举报