1. 程式人生 > >postman(八):使用newman來執行postman腳本

postman(八):使用newman來執行postman腳本

multi info cts execute 安裝 present exp lec localhost

通過之前的了解,我們知道postman是基於javascript語言編寫的,而導出的json格式的postman腳本也無法直接在服務器運行,它需要在newman中執行(可以把newman看做postman腳本的運行環境) 所以要實現在windows的cmd窗口或者linux系統中直接以命令的方式執行腳本,我們需要安裝node.js,然後再在此基礎上安裝newman 了解newman:https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/ 一.windows下安裝 1.安裝node.js 到官網下載最新的windows版node.js,直接安裝即可(不用自己去配置環境變量,安裝完成後會自動配好) (官網下載地址:https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz) 技術分享圖片
安裝完成後,打開cmd窗口,輸入 node -v,如下出現版本號表示安裝成功 技術分享圖片 2.安裝newman 可以通過npm來安裝newman,npm 是 JavaScript 的包管理工具,並且是 Node.js 平臺的默認包管理工具。通過 npm 可以安裝、共享、分發代碼,管理項目依賴關系。 一般安裝好node.js後會默認安裝好npm的,直接使用即可 打開cmd窗口,執行以下命令 npm -g install newman 安裝完成後,輸入newman -v,如下出現版本號表示安裝成功 技術分享圖片 3.如果想生成html格式的測試報告,還需要安裝 newman-reporter-html npm install -g newman-reporter-html 技術分享圖片
二.linux系統下安裝 安裝node.js 1.下載linux版本安裝包,因為後綴為.tar.xz,需要分兩步解壓 (官網下載地址:https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz) [root@localhost hanmk]# xz -d node-v10.15.1-linux-x64.tar.xz [root@localhost hanmk]# tar -xvf node-v10.15.1-linux-x64.tar 2.在環境變量中添加node.js路徑 打開/etc/profile文件,添加如下兩行 export NODE_HOME=/hanmk/node-v10.15.1-linux-x64 export PATH=$NODE_HOME/bin:$PATH 更新文件 [root@localhost node-v10.15.1-linux-x64]# source /etc/profile 3.查看是否安裝成功 [root@localhost node-v10.15.1-linux-x64]# node -v v10.15.1 安裝newman [root@localhost node-v10.15.1-linux-x64]# npm -g install newman [root@localhost node-v10.15.1-linux-x64]# newman -v 4.3.1 安裝newman-reporter-html [root@localhost postman_script]# npm install -g newman-reporter-html npm WARN [email protected] requires a peer of newman@4 but none is installed. You must install peer dependencies yourself. + [email protected] added 29 packages from 66 contributors in 13.266s newman的常用命令 官方的文檔介紹如下:https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/ 使用newman run 來執行腳本,先看下有哪些可選參數
[root@localhost bin]# newman run -h
Usage: run 
<collection> [options] URL or path to a Postman Collection. Options: -e, --environment <path> Specify a URL or Path to a Postman Environment. -g, --globals <path> Specify a URL or Path to a file containing Postman Globals. --folder <path> Specify folder to run from a collection. Can be specified multiple times to run multiple folders (default: []) -r, --reporters [reporters] Specify the reporters to use for this run. (default: ["cli"]) -n, --iteration-count <n> Define the number of iterations to run. -d, --iteration-data <path> Specify a data file to use for iterations (either json or csv). --export-environment <path> Exports the environment to a file after completing the run. --export-globals <path> Specify an output file to dump Globals before exiting. --export-collection <path> Specify an output file to save the executed collection --postman-api-key <apiKey> API Key used to load the resources from the Postman API. --delay-request [n] Specify the extent of delay between requests (milliseconds) (default: 0) --bail [modifiers] Specify whether or not to gracefully stop a collection run on encountering an errorand whether to end the run with an error based on the optional modifier. -x , --suppress-exit-code Specify whether or not to override the default exit code for the current run. --silent Prevents newman from showing output to CLI. --disable-unicode Forces unicode compliant symbols to be replaced by their plain text equivalents --global-var <value> Allows the specification of global variables via the command line, in a key=value format (default: []) --color <value> Enable/Disable colored output. (auto|on|off) (default: "auto") --timeout [n] Specify a timeout for collection run (in milliseconds) (default: 0) --timeout-request [n] Specify a timeout for requests (in milliseconds). (default: 0) --timeout-script [n] Specify a timeout for script (in milliseconds). (default: 0) --ignore-redirects If present, Newman will not follow HTTP Redirects. -k, --insecure Disables SSL validations. --ssl-client-cert <path> Specify the path to the Client SSL certificate. Supports .cert and .pfx files. --ssl-client-key <path> Specify the path to the Client SSL key (not needed for .pfx files) --ssl-client-passphrase <path> Specify the Client SSL passphrase (optional, needed for passphrase protected keys). -h, --help output usage information <collection>是指單個請求或者從postman導出的集合文件(也就是json格式的腳本) options是一些組合參數,介紹下我用到的幾個參數 (1) -e 指定環境變量,把在postman中設置的環境變量導出,然後再把路徑填寫到這裏即可 (2) -g 指定全局變量,把在postman中設置的全局變量導出,然後再把路徑填寫到這裏即可 (3) -n 指定叠代次數,即運行n次腳本 (4) --timeout-request 指定請求的超時時間 (5) -r 指定運行報告的格式,可以為json格式、html格式,默認為cli格式,即在命令行展示運行結果
實例1: 把接口測試腳本和環境變量腳本導出放到一個目錄中,在cmd窗口中切換到該目錄,執行如下命令 E:\5.coding\postman>newman run Test.postman_collection.json -n 2 -e base_url.postman_environment.json Test.postman_collection.json -- 接口測試腳本文件 base_url.postman_environment.json -- 環境變量文件 -n 2表示叠代2次 執行過程如下 技術分享圖片 執行完成後,會出現一個類似報表的東西,顯示整體運行結果 技術分享圖片 實例2: E:\5.coding\postman>newman run Test.postman_collection.json -e base_url.postman_environment.json --reporters cli,json,html --reporter-json-export report-json.json --reporter-html-export report-html.html --reporters cli,json,html --reporter-json-export report-json.json --reporter-html-export report-html.html 表示生成json和html格式的報告 html格式的報告長下面這個樣子,還是蠻難看的。 技術分享圖片

postman(八):使用newman來執行postman腳本