1. 程式人生 > >Web API 持續整合:PostMan+Newman+Jenkins

Web API 持續整合:PostMan+Newman+Jenkins

作者:葡萄城技術開發團隊

www.cnblogs.com/powertoolsteam/p/9881398.html

 

上篇文章我們已經完成了API測試工具選型,接下來是一系列週期性的開發測試過程:介面開發、檢出程式碼、執行測試、記錄結果、傳送報告。為了快速發現問題,並減少重複過程以節省時間、費用和工作量,我們需要一套完整的持續整合解決方案,除介面開發之外其他環節全部自動完成,無需太多的人工干預。

這篇文章將對持續整合解決方案進行一步一步的講解,希望對大家能有幫助。

 

1. Web Api 測試工具選型

目前市場有很多的用於API 測試的工具,如Postman, SoapUI, YApi, HttpRunner等等。

在進行了很多的嘗試後,我們最終還是決定在Postman和Soap UI間做出抉擇,在經過我們詳細的對比後:https://www.cnblogs.com/powertoolsteam/p/9772211.html

最終我們選擇了Postman。

 

2. 用Postman建立專案

選型做好了,第二步當然是Postman用起來了,建立自己的專案。參照Postman官網的文件。https://learning.getpostman.com/docs/postman/collections/intro_to_collections/

  • 建立 LeyserkidsApi專案

 

3. 原始碼管理

因為我們專案使用TFS做原始碼管理,這裡就以TFS為例。

  • 把Postman中專案LeyserkidsApi的Collection和Envrionment匯出為json檔案。

  • TFS建立專案LeyserKids,追加Postman匯出的JSON檔案,進行資源版本管理。接下來展示的專案結構如下:

 

4. Jenkins服務搭建

我們稱搭建Jenkins Server的機器為:機器A,後續步驟需要對這臺機器安裝一些Services。

 

5. Jenkins與TFS關聯

建立FreeStyle Project :Test

 

Jenkins主頁->Manage Jenkins->Manage Plugins->找到Team Foundation Server外掛並安裝。

 

進入Project Test詳細頁面->Configure->Source Code Management

 

Build Triggers 設定當原始碼發生變化時,重新構建。可以每天定時構建。

 

6. Jenkins構建(Build)

Newman是用命令列來執行Postman的Collection。可以很容易的和Jenkins整合。Newman是基於Nodejs的,所以首先得安裝Nodejs。

  • 在機器A(Jenkins Server)上安裝nodejs和newman。參照:https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman

  • 進入Test詳細頁面->Configure ->Build->Add Build Step->Execute Windows batch command->command

 

7. 郵件配置

Jenkins構建之後,傳送郵件,通知構建結果。郵件配置步驟如下:

Jenkins主頁->ManageJenkins->Manage Plugins->找到Email Extension Plugin外掛並安裝->外掛安裝之後需要重啟jenkins 服務

 

Jenkins主頁->Manage Jenkins->Configure System-> Extended E-mail Notification, 配置郵件Server

 

進入Test詳細頁面->Configure-> Post-build Actions->Add post-build action(Editable-Email Notification)

  • 進入Test詳細頁面

  • 對Test進行配置

  • Post-build Actions追加動作:Editable-Email Notification

  • 配置Editable-Email Notification

 

8. 專案構建

 

經過以上步驟的配置,構建完成之後,會收到郵件,根據連結在console output檢視構建結果,不是很直觀。下一步中將進行改善。

 

9. 更優美的郵件模板

預設的郵件內容過於簡單,用Email Extension Plugin 提供的Groovy標準HTML模板:groovy-html.template

進入Test詳細頁面->Configure-> Editable-Email Notification-> Default Content配置模板

重新Build,檢視郵件,郵件內容變的易讀行,傳送錯誤紅色顯示很是醒目。成功則顯示為綠色,一目瞭然。

 

10. 更簡潔精煉,美觀的構建報告:Html Report

檢視Console Output 中的構建結果,易讀性差,而且也不美觀,所以我們要改進。用newman生成HTML Report,再通過HTML Publisher外掛展示到Jenkins。

Jenkins主頁->ManageJenkins->Manage Plugins->找到HTML Publisher外掛並安裝。

在機器A(Jenkins Server)上通過Nodejs安裝reporter-html-template$ npm install -g newman-reporter-html

newman-reporter-html使用參見:https://github.com/postmanlabs/newman-reporter-html#readme

環境準備好了,把準備好的模板:htmlreqres.hbs上傳到TFS專案的templates下。我用了https://github.com/MarcosEllys/awesome-newman-html-template 這個小夥伴的模板,進行了些許改造。

進入Test詳細頁面->Configure-> Execute Windows batch command->command

把匯出的index.html放在了當前Job對於的newman資料夾(資料夾會自動建立)下。此路徑將在下一步配置中使用。

進入Test詳細頁面->Configure-> Post-build Actions->Add post-build action(Publish Html report)配置Html Report ,展示上一個步驟生成的Index.html檔案。

重新構建,檢視生成的HTML Report,結果如下,是不是好很多

  • 進入Test詳細,檢視最新Html Report

  • 檢視某次構建的Report,選中一個build history,檢視HTML Report。

  • Report詳細

如果你的Report樣式或JS指令碼載入不正確,解決方案如下:

https://testerhome.com/topics/9476

 

以上,內容比較多,寫的比較粗,歡迎交流。