1-测试 - Postman篇之命令行测试
before
命令行测试?难道GUI的还不能满足你么?非也非也,使用命令行模式为了搭配其他的工具来开展测试工作。
环境配置
在正式展开命令行测试之前,我们需要把相关环境配置一下。
安装node.js
node.js for windows
http://nodejs.cn/
https://nodejs.org/en/
配置淘宝的cnpm镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm -v # # 检查cnpm是否安装成功
C:\Users\Anthony>cnpm -v
[email protected] (C:\Users\Anthony\AppData\Roaming\npm\node_modules\cnpm\lib\parse_argv.js)
[email protected] (C:\Users\Anthony\AppData\Roaming\npm\node_modules\cnpm\node_modules\npm\lib\npm.js)
[email protected] (G:\software\nodejs\node.exe)
[email protected] (C:\Users\Anthony\AppData\Roaming\npm\node_modules\cnpm\node_modules\npminstall\lib\index.js)
prefix=C:\Users\Anthony\AppData\Roaming\npm
win32 x64 10.0.14393
registry=https://r.npm.taobao.org
安装newman
cnpm install newman --global # 命令行执行
C:\Users\Anthony>cnpm install newman --global
Downloading newman to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman_tmp
Copying C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman_tmp\[email protected]@newman to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman
Installing newman's dependencies to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman/node_modules
[1/20] [email protected] installed at node_modules\[email protected]@eventemitter3
[2/20] [email protected] installed at node_modules\[email protected]@chardet
[3/20] @postman/[email protected] installed at node_modules\_@[email protected]@@postman\csv-parse
[4/20] [email protected] installed at node_modules\[email protected]@filesize
[5/20] [email protected] installed at node_modules\[email protected]@colors
[6/20] [email protected] installed at node_modules\[email protected]@commander
[7/20] [email protected] installed at node_modules\[email protected]@lodash
[8/20] [email protected] installed at node_modules\[email protected]@cli-progress
[9/20] [email protected] installed at node_modules\[email protected]@async
[10/20] [email protected] installed at node_modules\[email protected]@semver
[11/20] [email protected] installed at node_modules\[email protected]@mkdirp
[12/20] [email protected] installed at node_modules\[email protected]@xmlbuilder
[13/20] [email protected] installed at node_modules\[email protected]@word-wrap
[14/20] [email protected] installed at node_modules\[email protected]@cli-table3
[15/20] [email protected] installed at node_modules\[email protected]@pretty-ms
[16/20] [email protected] installed at node_modules\[email protected]@serialised-error
[17/20] [email protected] installed at node_modules\[email protected]@postman-collection-transformer
[18/20] [email protected] installed at node_modules\[email protected]@postman-request
[19/20] [email protected] installed at node_modules\[email protected]@postman-collection
[20/20] [email protected] installed at node_modules\[email protected]@postman-runtime
Recently updated (since 2020-01-03): 3 packages (detail see file C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman\node_modules\.recently_updates.txt)
2020-01-07
→ [email protected] › [email protected] › uglify-js@^3.1.4(3.7.4) (09:24:25)
2020-01-06
→ [email protected] › mime-types@~2.1.19(2.1.26) (11:47:55)
→ [email protected] › [email protected] › [email protected](1.43.0) (11:24:37)
All packages installed (158 packages installed from npm registry, used 23s(network 23s), speed 390.2kB/s, json 139(361kB), tarball 8.25MB)
[[email protected]] link C:\Users\Anthony\AppData\Roaming\npm\newman@ -> C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman\bin\newman.js
检查newman是否安装成功
newman -v
# 示例
C:\Users\Anthony>newman -v
C:\Users\Anthony>"node" "C:\Users\Anthony\AppData\Roaming\npm\\node_modules\newman\bin\newman.js" -v
4.5.7
PS:扫盲篇
一句话:nodejs是一个基于 Chrome V8 引擎的 JavaScript 运行环境,node.js的包管理器npm是全球最大的开源库生态系统,我们可以使用npm命令来下在各种库和工具,但很遗憾,由于网络的原因,我们选择使用淘宝的npm镜像,叫做cnpm,而newman则是 Postman 推出的一个 nodejs 库,直接来说 newman 就是 Postman 的json文件可以在命令行中执行的插件,newman 可以方便地运行和测试集合,并用之构造接口自动化测试和持续集成。
使用newman执行命令行测试
使用newman执行命令行测试,大致要分为以下几步:
- 导出集合为json脚本
- 导出环境为json文件
- 准备好参数文件
- 安装reporter
- 执行newman命令
来走一波。
导出集合为json脚本
这里还是以之前的token的集合为例。按照下面的图操作即可。


将json文件导出到桌面。

(可选)导出环境为json文件
如果你的集合中的接口使用了环境变量,还需要将环境变量导出来,如果没有,就不用导了。

点击环境设置,选择对应的环境,然后点击下载,下载到本地的还是一个json文件。

(可选)准备好参数文件
同样的,我们在这个集合中,使用了数据驱动,也就是引用了data.csv文件,你也要把这个文件提前准备好。
安装reporter
啥也不说了,干(下载)就完了!
cnpm install -g newman-reporter-html
C:\Users\Anthony>cnpm install -g newman-reporter-html
Downloading newman-reporter-html to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman-reporter-html_tmp
Copying C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman-reporter-html_tmp\[email protected]@newman-reporter-html to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman-reporter-html
Installing newman-reporter-html's dependencies to C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman-reporter-html/node_modules
[1/4] [email protected] installed at node_modules\[email protected]@filesize
[2/4] [email protected] installed at node_modules\[email protected]@lodash
[3/4] [email protected] installed at node_modules\[email protected]@pretty-ms
[4/4] [email protected] installed at node_modules\[email protected]@handlebars
Recently updated (since 2020-01-03): 1 packages (detail see file C:\Users\Anthony\AppData\Roaming\npm\node_modules\newman-reporter-html\node_modules\.recently_updates.txt)
2020-01-07
→ [email protected] › uglify-js@^3.1.4(3.7.4) (09:24:25)
All packages installed (12 packages installed from npm registry, used 4s(network 4s), speed 380.84kB/s, json 12(55.18kB), tarball 1.34MB)
执行newman命令
参照下面的命令,开始执行吧!
newman run 脚本 -d 参数文件 -e 环境文件 -n 循环次数 -r html --reporter-html-export 测试报告路径
首先,由于保存的json文件名,又臭又长,还含有中文,我们稍事整理,将这些文件整理到一个目录中,比如我把它们放到桌面的postman_runner目录中。

在当前目录中打开终端,执行命令:
newman run token.json -d data.csv -e pe.json -n 2 -r html --reporter-html-export report.html

ok,执行完,就在本地生成了一个report.html文件,让我们打开它看一眼吧!

看不懂?感觉low?那是你没见过原生的.....
newman run token.json -d data.csv -e pe.json -n 2

我想以你的审美,上面这个会不会更好看?
question
安装cnpm失败
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm -v

可以看到报错,可能的原因是nodejs的版本太低,下面是原来的版本和新升级后的版本:
C:\Users\Anthony>node -v
v0.12.2
C:\Users\Anthony>node -v
v12.16.2
等我升级完毕nodejs后,重新下载安装vnpm即可。你这里可以去nodejs官网下载新版安装即可。
欢迎斧正,that's all

浙公网安备 33010602011771号