序
寫的api多了以后或者接手別人的項目之后,對api的運維也會比較多,特別是在測試環(huán)境,種種因素會導致接口出現(xiàn)不符合預期,這個時候當產(chǎn)品啊、測試啊,都跑過來騷擾你的時候,你的第一個反應是自己執(zhí)行一下,看是不是真的接口有問題,然后再具體分析。
通常是拼接好接口地址,構造好參數(shù),然后請求api,看看返回結(jié)果。這類動作做多了之后通常比較煩人,特別是最后發(fā)現(xiàn)是接口ok的。
于是就想找個rest api的自動化測試工具,方便自己排查問題。
工具選型
選型標準
- 能夠批量導入swagger
- 能夠自己構造測試接口
- 能批量run
- 能輸出report
- 最好能夠alert
候選工具
- jmeter——網(wǎng)上找了一圈,首先入眼的是jmeter,但是由于界面界面有點粗糙,學習成本有點高,故暫時沒有考慮。
- soupui——看起來是老牌的工具,不過由于免費版不支持導出report,也就放棄了
- dredd——這個看起來不錯,可以支持swagger的,不過自己粗略試一下,沒執(zhí)行成功,也就先放棄了
- postman——這個以前就有裝過,只是沒發(fā)現(xiàn)深挖它的功能,現(xiàn)在一看,挺簡單的,容易上手,也支持swagger,然后就是它了
postman相關知識
導入swagger
這個功能是我最看重的,左上角有個import的按鈕,可以選擇"Import From Link",輸入接口的swagger api docs的地址,比如:http://192.168.99.100:8080/scm/v2/api-docs,然后導入就可以了。
設置collection
保存一個請求的時候,可以選擇已有的collection,或者新建一個。collection可以對等為test suite。
設置tests
對于要跑自動化測試的,必須要設置tests這里的腳本,不然即使run,也沒有啥意義。最簡單最常用的兩行腳本如下:
tests["Status code is 200"] = responseCode.code === 200;
tests['Response time is less than 500ms'] = responseTime < 500;
一個是斷言http的狀態(tài)碼,一個是斷言響應時間。
設置環(huán)境變量
隨便選擇collection的一個http請求,然后點擊右側(cè)的設置按鈕,Manage Environments,然后可以定義環(huán)境變量,可以定義dev、prod兩套,分別設置對應的環(huán)境的api的host,這樣就不用重復設置api請求了。在url中用{{varname}}來引用變量,假設varname就是你設置的一個變量名。
執(zhí)行runner
左上角有個runner圖標,點一下彈出COLLECTION RUNNER界面。在這里就要進行批量自動測試的地方,選擇environment,然后跑一下。
newman命令行執(zhí)行
- 導出配置
在collection那里,export,選擇Collection V2,導出為json。如果使用了environment,則需要導出該environment的json配置。 - 安裝newman
sudo npm install -g newman
- 查看newman版本
newman -version
3.4.3
- 命令行執(zhí)行
newman run demo.postman_collection.json --reporters cli,html --environment dev.postman_environment.json --reporter-html-export result.html
命令行結(jié)果如下:
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 22 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 22 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 44 │ 6 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 28s │
├───────────────────────────────────────────────┤
│ total data received: 312.29KB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 1245ms │
└───────────────────────────────────────────────┘
# failure detail
1. AssertionFai… Response time is less than 1000ms
at assertion:2 in test-script
inside "XXXX" of "app1"
2. AssertionFai… Response time is less than 1000ms
at assertion:2 in test-script
inside "XXXX" of "app2"
3. AssertionFai… Response time is less than 1000ms
at assertion:2 in test-script
inside "XXXX" of "app1"
4. AssertionFai… Response time is less than 1000ms
at assertion:2 in test-script
inside "XXXX" of "app3"
5. AssertionFai… Response time is less than 1000ms
at assertion:2 in test-script
inside "XXXX" of
"app2"
6. AssertionFai… Status code is 200
at assertion:1 in test-script
inside "XXXX" of "app1"
同時會生成result.html報告。
jenkins集成
- 構建選擇Execute Windows batch command——輸入上面的命令就可以了
- Publish JUnit test result report——jenkins有個Publish JUnit test result report可以用來解析junit的xml測試報告。要用這個的話,命令行得輸出junit的report
newman run demo.postman_collection.json --reporters cli,html,junit --environment dev.postman_environment.json --reporter-html-export result.html --reporter-junit-export junit-result.xml