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
cnpm@6.1.0 (C:\Users\Anthony\AppData\Roaming\npm\node_modules\cnpm\lib\parse_argv.js)
npm@6.12.0 (C:\Users\Anthony\AppData\Roaming\npm\node_modules\cnpm\node_modules\npm\lib\npm.js)
node@8.9.0 (G:\software\nodejs\node.exe)
npminstall@3.23.0 (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\_newman@4.5.7@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] eventemitter3@4.0.0 installed at node_modules\_eventemitter3@4.0.0@eventemitter3
[2/20] chardet@0.8.0 installed at node_modules\_chardet@0.8.0@chardet
[3/20] @postman/csv-parse@4.0.2 installed at node_modules\_@postman_csv-parse@4.0.2@@postman\csv-parse
[4/20] filesize@6.0.1 installed at node_modules\_filesize@6.0.1@filesize
[5/20] colors@1.4.0 installed at node_modules\_colors@1.4.0@colors
[6/20] commander@4.0.1 installed at node_modules\_commander@4.0.1@commander
[7/20] lodash@4.17.15 installed at node_modules\_lodash@4.17.15@lodash
[8/20] cli-progress@3.4.0 installed at node_modules\_cli-progress@3.4.0@cli-progress
[9/20] async@3.1.0 installed at node_modules\_async@3.1.0@async
[10/20] semver@6.3.0 installed at node_modules\_semver@6.3.0@semver
[11/20] mkdirp@0.5.1 installed at node_modules\_mkdirp@0.5.1@mkdirp
[12/20] xmlbuilder@13.0.2 installed at node_modules\_xmlbuilder@13.0.2@xmlbuilder
[13/20] word-wrap@1.2.3 installed at node_modules\_word-wrap@1.2.3@word-wrap
[14/20] cli-table3@0.5.1 installed at node_modules\_cli-table3@0.5.1@cli-table3
[15/20] pretty-ms@5.1.0 installed at node_modules\_pretty-ms@5.1.0@pretty-ms
[16/20] serialised-error@1.1.3 installed at node_modules\_serialised-error@1.1.3@serialised-error
[17/20] postman-collection-transformer@3.2.0 installed at node_modules\_postman-collection-transformer@3.2.0@postman-collection-transformer
[18/20] postman-request@2.88.1-postman.16 installed at node_modules\_postman-request@2.88.1-postman.16@postman-request
[19/20] postman-collection@3.5.5 installed at node_modules\_postman-collection@3.5.5@postman-collection
[20/20] postman-runtime@7.21.0 installed at node_modules\_postman-runtime@7.21.0@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
→ postman-runtime@7.21.0 › handlebars@4.5.3 › uglify-js@^3.1.4(3.7.4) (09:24:25)
2020-01-06
→ postman-request@2.88.1-postman.16 › mime-types@~2.1.19(2.1.26) (11:47:55)
→ postman-request@2.88.1-postman.16 › mime-types@2.1.26 › mime-db@1.43.0(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)
[newman@4.5.7] 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\_newman-reporter-html@1.0.5@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] filesize@6.0.1 installed at node_modules\_filesize@6.0.1@filesize
[2/4] lodash@4.17.15 installed at node_modules\_lodash@4.17.15@lodash
[3/4] pretty-ms@5.1.0 installed at node_modules\_pretty-ms@5.1.0@pretty-ms
[4/4] handlebars@4.5.3 installed at node_modules\_handlebars@4.5.3@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
→ handlebars@4.5.3 › 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
