命令执行 ( newman )

问题思考
在前面我们都是在 postman 图形界面工具里面进行测试,但是有时候我们需要把测试脚本集成到 CI 平台,或者在非图形界面的系统环境下测试,那么该如何处理呢?
 
 
Newman 简介
Newman 是一款基于 Node.js 开发的可以运行 Postman 的工具,使用 Newman,可以直接从命令行运行和测试。
 
Postman 集合。
Newman : https://www.npmjs.com/package/newman#using-newman-as-a-nodejs-module
 
Newman 应用
环境准备
   • Node.js
  • cnpm 或 npm
  以上安装可以参考:我要自学网,Appium 环境搭建 。
 
 
配置好环境后,执行如下命令安装 newman
npm install newman --global
 
安装 newman-reporter-html 命令如下,如果不安装这个会报错 。
npm install -g newman-reporter-html

 

输入如下面命令检测安装是否成功。
C:\Users\27806>npm install -g newman-reporter-html
npm WARN newman-reporter-html@1.0.5 requires a peer of newman@4 but none is installed. You must install peer dependencies yourself.

+ newman-reporter-html@1.0.5
added 12 packages from 44 contributors in 1.833s

C:\Users\27806>npm install newman --global
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
C:\Users\27806\AppData\Roaming\npm\newman -> C:\Users\27806\AppData\Roaming\npm\node_modules\newman\bin\newman.js
+ newman@5.2.3
added 132 packages from 152 contributors, removed 483 packages and updated 24 packages in 13.001s

C:\Users\27806>newman -v
5.2.3

 

执行测试

首先将 postman 的集合导出,如下图所示:
 
014、Postman命令执行 ( newman )_html
 
在桌面新建文件夹 pmtest , 将导出的 postman 文件和相关数据文件 data.json 放入 pmtest 文件夹中 。
打开 cmd 进入到 pmtest 目录,输入如下命令 :
C:\Users\27806>cd C:\Users\27806\Desktop\pmtest

C:\Users\27806\Desktop\pmtest>newman run data_driver.postman_collection.json -d data.json -r html

C:\Users\27806\Desktop\pmtest>

支持 txt文件的数据,如下:

C:\Users\27806>cd Desktop\pmtest

C:\Users\27806\Desktop\pmtest>newman run future_loan_ddt.postman_collection.json -d test_login.txt -r html

 

命令说明

  • run 代表要执行的 postman 脚本,即为导出的集合。
  • -d 表示要执行的数据,也就是之前导入 postman 的数据
  • -r 生成的测试报告类型,这里生成 html 格式报告
更多命令用法请输入 newman -h 即可查看。

 

报告查看
在测试文件夹 pmtest 里面可以看到生成的一个 newman 文件夹,打开就可以看到生成的测试报告。
 
 
newman 不仅支持生成 html 报告,还支持其他报告类型:
  • JSON reporter
  • JUNIT/XML reporter
  • Client report
  • Html report

014、Postman命令执行 ( newman )_desktop_02

 

html报告如下:

014、Postman命令执行 ( newman )_html_03