天天看點

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

在上篇部落格裡寫了用newman來生成postman,現在來看怎麼把newman部署到Jenkins上

由于我這邊的測試環境早就安裝了Jenkins和node,是以就不多做贅述。

這篇部落客要說明怎麼在用Jenkins執行newman指令

一、在linux環境裡安裝Jenkins

下面放個其他熱點安裝Jenkins的連結

參考連結:https://blog.csdn.net/zjh_746140129/article/details/80835866

二、在linux環境安裝node.js

可供參考連結https://blog.csdn.net/sinat_37390744/article/details/77413673

運作

node -v

來檢視是否安裝成功

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

三、安裝newman

因為newman是 Postman 推出的一個 nodejs 庫,是以要運作它必須安裝nodejs,那newman也可以直接用npm來安裝

npm install -g newman

如下圖,安裝完畢會出現安裝的newman的版本,還有安裝所在的路徑

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

安裝完的newman就在npm所在的檔案夾裡

運作

newman -v

來檢查是否安裝成功

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

給newman建立一個軟連接配接

ln -s /var/lib/jenkins/env/node-v10.4.1-linux-x64/bin/newman /usr/bin/newman

安裝html

npm install -g newman-reporter-html

四、從postman裡導出collection檔案

點選用例集右側的三個點,

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

點選Export

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

五、直接在linux裡運作newman

講導出的json檔案放入到linux環境裡,包括用例和環境變量

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

執行

newman run 1.json -g env.json

結果如下圖

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

注意:要看把導出的json檔案放在哪個路徑了,運作的路徑要對,我這邊就放在根目錄裡,也是在根目錄下運作的。

生成html檔案

newman run 1.json -g env.json -r html

生成的html檔案就在同目錄的newman檔案夾裡

六、在Jenkins裡運作腳本

打開Jenkins

建立一個自由風格的項目

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

在建構裡選擇Execute shell

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

在指令裡輸入

cd /

newman run 1.json -g env.json

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

這裡的指令其實就跟在linux環境裡操作一樣

點選儲存

進入這個項目,點選Bulid Now

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

我執行期間遇到一個問題

就是在Jenkins裡一直報錯:/usr/bin/env: node: 沒有那個檔案或目錄

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

這個問題是因為在newman腳本最上方的node位置導緻的

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

要把這裡改成node檔案所在的路徑

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

這時候再去執行Jenkins項目

就會成功啦,如下圖

postman+newman+Jenkins自動化測試接口生成測試報告,包括遇到的錯誤與問題

每次都很開心把所有學習經曆和遇到的錯誤寫上部落格與大家分享~

下一步就是要配置Jenkins自動發送測試報告的郵件了

繼續閱讀