# Postman 介紹 ###### tags: `http` `https` `api test` `postman` Postman 是功能強大的 HTTP/HTTPS 測試工具 支援 GET/POST/PUT/DELETE/PATH/HEAD 等方法 ## Test Script 可以使用 Postman 進行 API 測試 ![](https://i.imgur.com/np1igr6.jpg) ### 方式 1. 點選 **Tests** 標籤 2. 填寫 Test Script 3. 案 **Send** 4. 點選 **Test Results** 標籤觀看測試結果 ### 常用測試案例 #### 測試是否 `200 OK` ```script pm.test("status 200" ,function () { pm.response.to.be.ok; }); ``` #### 取得 Response Body 的 JSON 的值 ```script pm.test("get token from JSON Body and compare with global data" ,function () { var jsonData = pm.response.json(); var token = jsonData.token; var _token = pm.globals.get("TOKEN"); pm.expect(token).to.eql(_token); }); ``` #### 設定全域變數 ```script pm.globals.set("variable_key", "variable_value"); ``` #### 取得全域變數 ```script var value = pm.globals.get("variable_key"); ``` #### Response time is less than 200ms ```script pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); ```` #### Status code is 200 ```script pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); ``` ## 增加範例 這個功能在生成文件及Mock API Server 很重要 ![](https://i.imgur.com/drS0Np9.png) ![](https://i.imgur.com/XsfKhF6.png) ## 生成 Mock Collection ![](https://i.imgur.com/FxNyT1f.png =300x) ## 生成文件 ![](https://i.imgur.com/4jG7KJG.png =300x) ![](https://i.imgur.com/lg8hK1e.png) ## 參考 - https://learning.getpostman.com/docs/postman/scripts/test-examples/