# Postman 介紹 ###### tags: `http` `https` `api test` `postman` Postman 是功能強大的 HTTP/HTTPS 測試工具 支援 GET/POST/PUT/DELETE/PATH/HEAD 等方法 ## Test Script 可以使用 Postman 進行 API 測試  ### 方式 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 很重要   ## 生成 Mock Collection  ## 生成文件   ## 參考 - https://learning.getpostman.com/docs/postman/scripts/test-examples/
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up