Postman(12): newman的簡介與使用


newman簡介

newman是為Postman而生,專門用來運行Postman編寫好的腳本; 使用newman,你可以很方便的用命令行來執行postman collections。

newman的安裝

1.先下載Node.js;https://nodejs.org/en/

 

 

 

2.安裝NodeJs(很容易安裝,這里不多做介紹)

3.打開cmd,輸入node,如果沒有報錯,而是顯示“>”,說明node安裝成功。

 

 

 

4.安裝newman;打開cmd,輸入npm install -g newman

 

 

 

5.通過查看newman版本測試安裝是否成功,打開cmd,輸入newman -v,出現 版本信息即安裝成功

 

 

 

newman的使用

步驟流程:

1、先新建一個文件夾;在文件夾中再新建存放不同文件的文件夾;如下圖

 

2、在postman中導出請求的集合;如下圖

 

 

 3、打開dos窗口,使用newman命令來執行postman導出的腳本;進入到腳本的目錄層級然后使用newman命令

 

 

 4、通過newman url地址也可以執行;如下圖

 

 

部分命令如下:

 

1、newman run 集合名/url地址 :運行一個集合,url地址為集合文件導出的url

 

 

 


2、-e,--environment :指定環境文件路徑

前置條件:把環境變量導出到env文件夾中;如下圖:

 

 環境變量的展示

 

 引用環境變量的展示

 

 執行命令:newman run 集合名 -e 環境文件路徑;如下圖

 

 

3、-g,--globals :指定全局變量的文件路徑

前置條件:把全局變量文件導出文件夾中;如下圖

 

 

 

 request請求中引用全局變量;如下圖

 

 執行newman命令;newman run 集合名 -e 環境文件路徑 -g 全局變量文件路徑

 

 

4、-d,--iteration-data :指定用於迭代的數據源文件路徑

5、-n,--iteration-count :指定迭代次數

6、--folder :運行集合中指定的文件夾

7、--timeout (ms) :設置整個集合運行完成執行的時間
8、-r,--reporters :指定用於此次運行的報告類型 cli,xml,json,junit,html,htmlextra
9、 --reporter-json-export     jsonReport.json     生成json格式的測試報告
10、--reporter-junit-export   xmlReport.xml       生成xml格式的測試報告
11、--reporter-html-export   htmlReport.html    生成html格式的測試報告
12、--reporter-htmlextra-export   htmlReport.html   生成htmlextra格式的測試報告

 

參數化文件通過newman命令執行

前置條件:新建一個csv文件;如下圖

 在request中引入csv文件中的變量;如下圖

並把該reques請求導出到script文件中;如下圖

 

 

 

 

 

 執行newman命令:newman run 集合路徑 -d 數據源文件路徑 -n 迭代次數 --folder 集合中指定的文件夾

 

通過newman命令生成測試報告;

一、生成html測試報告

使用命令:--reporter-html-export   htmlReport.html    生成html格式的測試報告

第一次執行測試報告會提示安裝插件,聯網安裝即可: npm install -g newman-reporter-html;如下圖

 安裝完成后再次執行newman命令生成html測試報告

查看reports文件夾下的html文件並打開

 二、生成加強版html測試報告和json測試報告

使用命令:

-r,--reporters :指定用於此次運行的報告類型 cli,xml,json,junit,html,htmlextra
 --reporter-json-export     jsonReport.json     生成json格式的測試報告
--reporter-htmlextra-export   htmlReport.html   生成htmlextra格式的測試報告

第一次執行測試報告會提示安裝插件,聯網安裝即可: npm install -g newman-reporter-htmlextra;如下圖

 

 安裝完成后再次執行newman命令生成html測試報告

 

 查看reports文件夾下的html文件並打開

 

 

常用newman命令應用如下

1、newman run postman導出文件(支持鏈接形式)
2、newman run 導出文件 -e 環境變量文件路徑
3、newman run 導出文件 -e 環境變量文件路徑 -d csv/json參數化文件 -n 次數
4、newman run 導出文件 -e 環境變量文件路徑 -d csv/json參數化文件 -r html --reporter-html-export html報告路徑 -n 次數
5、newman run 導出文件 -g 全局變量文件路徑 -r htmlextra --reporter-htmlextra-export html報告路徑

備注:在執行第4、5兩條命令時,由於沒有安裝html報告模塊會提示無法執行報 錯,此時根據提示執行命令聯網安裝即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM