postman+jenkins+newman做接口測試的持續集成

一。安裝node.js

下載地址:https://nodejs.org/en/  ,雙擊後根據提示下一步安裝完成

在dos窗口中輸入 :node -v 查看,如下圖所示表示安裝好了

二.重新打開cmd,輸入如下指令:


npm install --global --production windows-build-tools

安裝這些依賴需要一些時間,耐心等待安裝完成。

三.然後安裝Newman:


輸入命令:npm install -g newman

安裝會持續幾分鐘,直到出現下面界面,安裝結束

通過查看newman版本測試安裝是否成功

輸入命令:newman -v

四.安裝Postman

Postman功能(https://www.getpostman.com/features) 
   主要用於模擬網絡請求包 
   快速創建請求 
   回放、管理請求 
   快速設置網絡代理 
postman 下載地址:https://www.getpostman.com 
安裝後,導出Collection 
在postman中執行測試用例通過後,將postman中的用例導出,並用Newman執行,如下:

五.Newman 執行腳本

命令行運行collection 
cmd進入到collection的json文件的目錄下,輸入指令 
newman run postmanEcho.postman_collection.json

例子1,通過newman 運行postman導出的test1.json文件,並生成多種測試報告(json,junit的xml,html):

newman run c:\test1.json –reporters cli,html,json,junit –reporter-json-export jsonOut.json –reporter-junit-export xmlOut.xml –reporter-html-export htmlOut.html

例子2,運行https://www.getpostman.com/collections/cb0cea0af1467c8008fb(postman生成的 )中的所有api,並使用env.json作爲環境變量和globals.json作爲全局變量,並使用外部data.csv作爲外部數據,最後設置了接口請求超時時間爲5S 。

newman run https://www.getpostman.com/collections/cb0cea0af1467c8008fb 
–environment env.json –iteration-data data.csv –globals globals.json –timeout-request 5000

例子3 
newman run C:\11\testpm.postman_collection.json –reporter-html-export C:\11\htmlOut.html -n 10 
迭代10次

六.Newman語法

1、運行一個集合

newman run 集合名 
newman run url地址

2、-e,–environment 指定環境文件路徑或者url 
  -e的用法是如果我們的集合設置到環境變量,那麼但是運行集合文件是報錯的,需要使用-e指定才能運行集合,比如運行測試搜索集合

newman run d:\test.postman_collection.json -e d:\test.postman_environment.json

3、-g,–globals 指定全局變量的文件地址或url 用法如-e

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

newman run d:\test.postman_collection.json -e d:\test.postman_environment.json -d d:\testdata.csv

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

newman run 集合名 -n 10(迭代次數)

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

 如集合下存在文件(前端接口),使用該命名,則只運行集合中前端接口文件下的接口

7、–export-environment 導出該運行集合中的環境變量文件 可指定路徑和名稱 

newman run d:\test.postman_collection.json -e d:\test.postman_environment.json -d d:\testdata.csv —-export-environment d:\test

–export -environment d:\test 導入環境變量文件名稱爲test 存儲在d盤下  

注意:

將該文件導入postman之後還是與導出前的文件名相同,比如此處【test.postman_environment.json】導出前對應的環境變量爲【test】,那麼test導入之後的命名還是【test】,同樣,導出集合也是如此。 

8、–export-globals 導出全局環境變量

9、–export-collection 導出集合文件

10、–timeout (ms) 設置整個集合運行完成執行的時間

11、–timeport-request (ms) 指定等待請求返回響應的時間

12、–timeout-script (ms)指定等待腳本執行完成的時間

七.結合Jenkins

作用: 
    1.持續、自動地構建/測試軟件項目。 
    2.監控一些定時執行的任務。

Jenkins下載地址 https://jenkins.io/download/ 
步驟一:在Jenkins 機器上安裝Newman 
步驟二:搭建Jenkins環境,並新建個自由風格的Job 
步驟三:進入http://localhost:8080 
步驟四:構建選擇Execute Windows batch command,並輸入newman 運行命令

點擊完成後,在點擊立即構建

備註: 
可能在執行的時候 出現 newman指令非內部或外部指令 
解決方法: 

jenkins-系統設置-節點管理

點擊新增,添加nodejs、npm環境變量

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章