本文由葡萄城技術團隊於OSChina原創並首發
轉載請註明出處:葡萄城官網,葡萄城爲開發者提供專業的開發工具、解決方案和服務,賦能開發者。
上篇文章我們已經完成了API測試工具選型,接下來是一系列週期性的開發測試過程:接口開發、檢出代碼、運行測試、記錄結果、發送報告。爲了快速發現問題,並減少重複過程以節省時間、費用和工作量,我們需要一套完整的持續集成解決方案,除接口開發之外其他環節全部自動完成,無需太多的人工干預。
這篇文章將對持續集成解決方案進行一步一步的講解,希望對大家能有幫助。
1. Web Api 測試工具選型
目前市場有很多的用於API 測試的工具,如Postman, SoapUI, YApi, HttpRunner等等。
在進行了很多的嘗試後,我們最終還是決定在Postman和Soap UI間做出抉擇,在經過我們詳細的對比後:https://my.oschina.net/powertoolsteam/blog/2243550
最終我們選擇了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
- 進入Test詳細頁面
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
以上,內容比較多,寫的比較粗,歡迎交流。
- 進入Test詳細,查看最新Html Report