最近項目因爲SIT和UAT環境的服務時不時被反饋不太穩定,公司的Kibana監控也沒有預警到,項目組討論自己上一些監控功能其中有一項就是通過postman實現對接口的測試和預警,在這裏剛好把postman的功能也梳理和彙總下。
1. postman env
1.1 全局環境變量
全局變量一般用來設置一些公共的變量,postman的全局變量的設置 Environments -> Globals
全局變量的引用{{xxxx}},鼠標放在變量上顯示G圖標就是引用的全局變量
postman默認的全局變量如下表
1.2 環境變量
環境變量相對於全局變量來說,影響的範圍更小,但是優先級比全局變量更高,一般來說的不同的Collections會對應不同Enviroment環境變量文件,環境變量的設置如下圖創建一個名爲postman-skill的環境變量
引用環境變量,會發現有同樣命名爲{{mock-server}}的變量,環境變量優先級高於全局變量,值顯示爲“localhost:8980”
2. postman Tests
2.1 後置處理
postman的Tests可以理解成後置處理,postman還有一個前置處理 Pre-request Script, 這兩個分別表示在run test之前和之後運行的邏輯,此處講講Test後置處理
2.1.1 處理返回的狀態碼
pm.test("校驗response code爲 200", function () { pm.response.to.have.status(200); });
2.1.2 處理返回的Response Body
pm.test("校驗response boay包含 雙色球", function () { pm.expect(pm.response.text()).to.include("雙色球"); }); pm.test("校驗pageCount值爲 1 ", function () { var jsonData = pm.response.json(); pm.expect(jsonData.pageCount).to.eql(1); }); pm.test("校驗result數組的第一個對象的code值爲 2022026 ", function () { var jsonData = pm.response.json(); console.log(jsonData); pm.expect(jsonData.result[0].code).to.eql("2022026"); });
實際的Response Body
{ "state": 0, "message": "查詢成功", "pageCount": 1, "countNum": 0, "Tflag": 0, "result": [ { "name": "雙色球", "code": "2022026", "detailsLink": "/c/2022/03/10/499007.shtml", "videoLink": "/c/2022/03/10/499006.shtml", "date": "2022-03-10(四)", "week": "四", "red": "03,04,10,15,22,24", "blue": "02", "blue2": "", "sales": "390646552", "poolmoney": "1820096321", "content": "內蒙古1注,江蘇1注,浙江2注,福建1注,山東1注,廣東2注,海南1注,深圳1注,共10注。", "addmoney": "", "addmoney2": "", "msg": "", "z2add": "", "m2add": "", "prizegrades": [ { "type": 1, "typenum": "10", "typemoney": "7519808" }, { "type": 2, "typenum": "129", "typemoney": "244167" }, { "type": 3, "typenum": "1096", "typemoney": "3000" }, { "type": 4, "typenum": "60656", "typemoney": "200" }, { "type": 5, "typenum": "1226877", "typemoney": "10" }, { "type": 6, "typenum": "7547686", "typemoney": "5" }, { "type": 7, "typenum": "", "typemoney": "" } ] }, { "name": "雙色球", "code": "2022025", "detailsLink": "/c/2022/03/08/498795.shtml", "videoLink": "/c/2022/03/08/498794.shtml", "date": "2022-03-08(二)", "week": "二", "red": "06,19,24,25,28,32", "blue": "04", "blue2": "", "sales": "380028752", "poolmoney": "1800801596", "content": "湖南1注,共1注。", "addmoney": "", "addmoney2": "", "msg": "", "z2add": "", "m2add": "", "prizegrades": [ { "type": 1, "typenum": "1", "typemoney": "10000000" }, { "type": 2, "typenum": "98", "typemoney": "261907" }, { "type": 3, "typenum": "1184", "typemoney": "3000" }, { "type": 4, "typenum": "65320", "typemoney": "200" }, { "type": 5, "typenum": "1271944", "typemoney": "10" }, { "type": 6, "typenum": "10842155", "typemoney": "5" }, { "type": 7, "typenum": "", "typemoney": "" } ] }, { "name": "雙色球", "code": "2022024", "detailsLink": "/c/2022/03/06/498683.shtml", "videoLink": "/c/2022/03/06/498682.shtml", "date": "2022-03-06(日)", "week": "日", "red": "07,09,10,14,19,24", "blue": "15", "blue2": "", "sales": "412478136", "poolmoney": "1733800692", "content": "山東1注,湖北1注,共2注。", "addmoney": "", "addmoney2": "", "msg": "", "z2add": "", "m2add": "", "prizegrades": [ { "type": 1, "typenum": "2", "typemoney": "10000000" }, { "type": 2, "typenum": "154", "typemoney": "187504" }, { "type": 3, "typenum": "1455", "typemoney": "3000" }, { "type": 4, "typenum": "82140", "typemoney": "200" }, { "type": 5, "typenum": "1599856", "typemoney": "10" }, { "type": 6, "typenum": "9964007", "typemoney": "5" }, { "type": 7, "typenum": "", "typemoney": "" } ] }, { "name": "雙色球", "code": "2022023", "detailsLink": "/c/2022/03/03/498667.shtml", "videoLink": "/c/2022/03/03/498666.shtml", "date": "2022-03-03(四)", "week": "四", "red": "07,12,17,19,24,25", "blue": "02", "blue2": "", "sales": "386299738", "poolmoney": "1667173674", "content": "山西1注,吉林1注,浙江1注,湖北1注,廣東5注,共9注。", "addmoney": "", "addmoney2": "", "msg": "", "z2add": "", "m2add": "", "prizegrades": [ { "type": 1, "typenum": "9", "typemoney": "7444170" }, { "type": 2, "typenum": "98", "typemoney": "280580" }, { "type": 3, "typenum": "1288", "typemoney": "3000" }, { "type": 4, "typenum": "70720", "typemoney": "200" }, { "type": 5, "typenum": "1393074", "typemoney": "10" }, { "type": 6, "typenum": "9472092", "typemoney": "5" }, { "type": 7, "typenum": "", "typemoney": "" } ] }, { "name": "雙色球", "code": "2022022", "detailsLink": "/c/2022/03/01/498455.shtml", "videoLink": "/c/2022/03/01/498454.shtml", "date": "2022-03-01(二)", "week": "二", "red": "01,07,11,15,17,19", "blue": "06", "blue2": "", "sales": "374172850", "poolmoney": "1651680455", "content": "北京1注,河北1注,江蘇3注,浙江1注,湖北1注,四川1注,寧夏1注,共9注。", "addmoney": "", "addmoney2": "", "msg": "", "z2add": "", "m2add": "", "prizegrades": [ { "type": 1, "typenum": "9", "typemoney": "6751389" }, { "type": 2, "typenum": "162", "typemoney": "121624" }, { "type": 3, "typenum": "1841", "typemoney": "3000" }, { "type": 4, "typenum": "95683", "typemoney": "200" }, { "type": 5, "typenum": "1710271", "typemoney": "10" }, { "type": 6, "typenum": "12553973", "typemoney": "5" }, { "type": 7, "typenum": "", "typemoney": "" } ] } ] }
2.1.3 處理返回的Request Headers
pm.test("校驗請求頭包含 X-XSS-Protection", function () { pm.response.to.have.header("X-XSS-Protection"); });
2.1.4 處理Reponse響應時間
pm.test("校驗響應時間小於 100ms", function () { pm.expect(pm.response.responseTime).to.be.below(100); });
2.1.5 設置變量
比如我這邊test case #7 和test case #8,有關聯關係,#8的請參數,需要先從#7的Response Body獲取
#7 set env
pm.test("Status code is 200", function () { pm.response.to.have.status(200); var resJson = pm.response.json(); console.log(resJson.token); if(resJson.token){ pm.environment.set("seanTokenId", resJson.token); } console.log(pm.environment); console.log(pm.environment.get("seanTokenId")); });
#8 get env
以上只是一部分postman的script的處理以及斷言等,但對開發人員來講基本也夠用了
3. postman newman
3.1 自動化測試
newman是postman的命令行Collection Runner,它直接從命令行運行和測試postman collection,並導出結果報表,關於怎麼安裝newman就不說了, 如果要生成html的報表還需要安裝newman-reporter-html
npm install -g newman-reporter-html
運行測試用例並導出結果報表爲html格式
newman run postman-skill.postman_collection.json -e postman-skill.postman_environment.json -r cli,html --reporter-html-export newman-report/postman-skill-report.html
如果對newman 參數不清可以看看help
newman run -h
選項:
-e, --environment <path> 指定Postman環境的URL或路徑
-g, --globals <path> 指定包含Postman Globals的文件的URL或路徑
-r, --reporters [reporters] 指定用於此運行的報告器(默認值:["cli"])
-n, --iteration-count <n> 定義要運行的迭代次數
-d, --iteration-data <path> 指定用於迭代的數據文件(JSON 或 CSV)
--folder <path> 指定要從集合運行的文件夾。可多次指定運行多個文件夾(默認:[])
--global-var <value> 允許通過命令行指定全局變量,採用 key=value 格式(默認值:[])
--env-var <value> 允許通過命令行指定環境變量,採用 key=value 格式(默認值:[])
--export-environment <path> 完成運行後將最終環境導出到文件
--export-globals <path> 完成運行後將最終的全局變量導出到文件
--export-collection <path> 完成運行後將執行的集合導出到文件
--postman-api-key <apiKey> API Key用於從Postman API加載資源
--bail [modifiers] 指定是否在遇到錯誤時優雅地停止收集運行,以及是否基於可選修飾符以錯誤結束運行
--ignore-redirects 防止Newman自動跟隨3XX重定向響應
-x , --suppress-exit-code 指定是否覆蓋當前運行的默認退出代碼
--silent 阻止Newman向CLI顯示輸出
--disable-unicode 強制將符合Unicode的符號替換爲其純文本等效項
--color <value> 啓用/禁用彩色輸出 (auto|on|off) (默認: "auto")
--delay-request [n] 指定請求之間的延遲程度(毫秒)(默認值:0)
--timeout [n] 指定收集運行的超時時間(毫秒)(默認值:0)
--timeout-request [n] 指定請求的超時時間(毫秒)(默認值:0)
--timeout-script [n] 指定腳本的超時時間(毫秒)(默認值:0)
--working-dir <path> 指定工作目錄的路徑
--no-insecure-file-read 防止讀取位於工作目錄之外的文件
-k, --insecure 禁用SSL驗證
--ssl-client-cert-list <path> 指定客戶端證書配置(JSON)的路徑
--ssl-client-cert <path> 指定客戶端證書 (PEM) 的路徑
--ssl-client-key <path> 指定客戶端證書私鑰的路徑
--ssl-client-passphrase <passphrase> 指定客戶端證書密碼(用於受保護的密鑰)
--ssl-extra-ca-certs <path> 指定額外受信任的CA證書 (PEM)
--cookie-jar <path> 指定自定義cookie jar的路徑(序列化的tough-cookie JSON)
--export-cookie-jar <path> 完成運行後將cookie jar導出到文件
--verbose 顯示收集運行和發送的每個請求的詳細信息
-h, --help 顯示命令幫助
上面已經可以通過指令法方式跑全部的測試用例,如果要進步實現自動化測試方案如下
1. postman 自帶的montiors - 這種方式比較快捷方便,可以實現定時跑測試用例,並自帶郵件功能,缺點是有些公司是內網開發,這個功能不能使用 2. jenkins集成newman - 這種方式也是方便的定製化需求,但是開發團隊需要依賴devops團隊的支撐,能落地的時間不確定 3. 自己實現一個自動測試程序(js ,java) - 這種方式完全由開發團隊自己來完成,對外部依賴較少,可以完全自己自由實現,就是需要投入開發時間
關於postmen的全部技巧可以參考官方文檔:https://learning.postman.com/docs/getting-started/introduction/