Newman基本使用


簡介

  Newman 是 Postman 推出的一個 nodejs 庫,直接來說就是 Postman 的json文件可以在命令行執行的插件。
  Newman 可以方便地運行和測試集合,並用之構造接口自動化測試和持續集成。

安裝

  安裝需要通過 npm 命令來完成,可以直接安裝 nodejs,配置環境變量來完成。
  在控制台中運行命令:npm install -g newman

 

  驗證newman安裝成功,使用命令: newman --version

 

使用

  官網

  newman 官網幫助文檔:https://www.npmjs.com/package/newman

 

   運行

  運行命令格式:newman run [options] <collection> URL or path to a Postman Collection
  運行的可以是Postman 集合導出的json文件,也可以是上傳到服務器的url

  簡單的運行從postman導出的json文件

  newman run APITest.postman_collection.json  

 

簡單的執行可以看出請求的接口、請求的方法、斷言結果以及結果統計。比較詳細。

  輸出測試報告文件

  newman 支持四種格式報告文件:cli,json,html,junit
  生成 html 報告時需要安裝html套件,命令行中執行:

  npm install -g newman-reporter-html

  

  輸出報告時使用的命令:
    -r html,json,junit         指定生成html,json,xml形式的測試報告
    --reporter-json-export jsonReport.json          生成json格式的測試報告
    --reporter-junit-export xmlReport.xml            生成xml格式的測試報告
    --reporter-html-export htmlReport.html          生成html格式的測試報告
    默認生成的測試報告保存在當前目錄下,如果文件名前加上路徑,則保存在指定的目錄下

  例:輸出json和html文件報告

  newman run APITest.postman_collection.json -r html,json --reporter-json-export jsonReport.json --reporter-html-export htmlReport.html

  

  執行命令后生成結果

  

  Html文件展示結果(有點low):

 

  htmlextra 使用

  使用htmlextra套件生成比較高級的html報告
  使用方法和html的相同,需要安裝htmlextra,命令行中輸入:

  npm install -g newman-reporter-htmlextra

  

  命令行中執行命令生成測試報告

  newman run APITest.postman_collection.json -r htmlextra --reporter-html-export htmlReport.html

  

  執行后生成測試報告文件

  

  生成的Html結果如下:

 

  

  

  通過頭部的Summary、Total Requests 、Failed Tests 、Skipped Tests 查看統計數據、所有的請求、失敗的用例、跳過的用例等。

  如下圖,所有的用例:

   

   點擊每個請求可以查看詳細,如下圖:

 

  如下圖,失敗的用例:

 

 

  設置重復執行次數
    例:重復執行10次

  newman run APITest.postman_collection.json -n 10

 

  查看其他可用的參數

  newman run -h

 

 

 

以上是 newman 的基本使用, newman 還可以結合 Jenkins 持續集成

 


免責聲明!

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



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