摘抄自: https://www.jianshu.com/p/132db486981e
1. postman的斷言解釋
http://www.51testing.com/html/01/n-3724901.html 對postman內置斷言的說明。

postman
2. 斷言語句使用舉例
- 以如下get請求舉例
https://www.v2ex.com/api/topics/hot.json
Method: GET
Authentication: None
- 發送請求
postman接口請求
- 如何驗證返回結果?
- 判斷請求的返回狀態碼為200(200表示請求正常)
pm.test("判斷返回狀態為200", function (){ pm.response.to.have.status(200); });
2.判斷返回元素中是否有node
pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("node"); });
3.判斷返回的的第一篇root的值為false
pm.test("判斷root為false", function () { var jsonData = pm.response.json(); pm.expect(jsonData[0].node.root).to.eql(false);});

斷言結果
- 如何使用postman 控制台進行輸出
打開postman console(點擊“View->Show Postman Console”)
console.log(jsonData.length)

image.png
- 如何設置環境變量
將返回的id值設為當前環境變量
pm.environment.set("id", jsonData[0].node.id);

image.png
在Test下添加如下代碼可將返回的id值設為全局環境變量
pm.globals.set("global_id", jsonData[0].node.id);

image.png