Postman helps you develop APIs faster.
豪華午餐
Postman
構建、管理、文檔化
API
- 在線安裝
- Chrome插件版
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop - MAC版
https://www.getpostman.com/app/postman-osx - 離線安裝
使用已經安裝好的Postman文件夾,機器
默認存放目錄:/Library/Application\ Support/Google/Chrome/Default/Extensions
進入chrome://extensions/
,選擇Load unpacked extensions
,加載Postman的文件夾
Newman
Newman is a command-line collection runner forPostman. It allows you to effortlessly run and test a Postman collection directly from the command-line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.
- 安裝:
newman
:npm install -g newman
- 功能: 直接運行Postman的接口Collection數據(本地或遠程)
- 運行: 本地Postman保存的Collection內容
? Downloads newman -c Demo.postman_collection.json
Iteration 1 of 1
200 1479ms Open the url of Baidu. [GET] http://www.baidu.com
? Status code is 200
Summary:
Parent Pass Count FailCount
-------------------------------------------------------------
Collection Demo 1 0
Total 1 0
- 運行遠程Postman
? ~ newman -u https://www.getpostman.com/collections/676a42223e14bf54ad84
Iteration 1 of 1
200 240ms Open the url of Baidu. [GET] http://www.baidu.com
? Status code is 200
Summary:
Parent Pass Count FailCount
-------------------------------------------------------------
Collection Demo 1 0
Total 1 0
Interceptor
Postman interceptor brings the power of your Chrome window to Postman! You can set custom headers (including cookies) from within Postman, and view cookies already set on the domain. You can also capture requests being sent from Chrome and import them into Postman. This makes building APIs a breeze!
- 安裝
https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo/ - 功能
- 記錄瀏覽器請求并直接導入到Postman
- 可添加
Filter
,對瀏覽器中的請求進行過濾
初級使用
錄制API-Interceptor
- 設置瀏覽器中的
Interceptor
:打開開關
、設置Filter
(僅過濾www.lxweimin.com
)
Chrome中設置Interceptor - 設置
Postman
中的Interceptor
:打開開關
Postmsn中打開Interceptor - 開始
錄制
:在Chrome
瀏覽器中輸入http://www.lxweimin.com/p/4a677c5f79f1,回車后,查看Postman
的請求錄入
錄制API結果
回放API-Postman
- 選擇
Postman
中的API:Get http://www.lxweimin.com/p/4a677c5f79f1
指定API - 點擊
Send
,進行回放
。可查看到請求的返回狀態碼200
、數據Body
回放結果
指定API請求-Postman
- 在
URL
輸入框中,輸入目標地址
:http://www.lxweimin.com/p/4a677c5f79f1。點擊Send
按鈕
1步搞定指定API請求
高級使用
規劃管理API - Postman
+Collection
將所有的API進行分類管理,如按模塊、系統、類型。
保存API至Collection
- 選中需要保存的API,點擊請求列表中的
Save to collection
Save to collection - 可將此API保存至
已有的Collection
或新增Collection
重命令API請求-便于管理和查看
- 切換到
Collections
列表,點擊API請求的操作區,選擇Edit
Edit-API信息
常用權限應用盡有-Authorization
- Postman在請求時,可指定
此次
請求Auth方式 - 可設置Auth中的具體內容,完全自行
定義
。如設置Basic Auth
中的用戶名和密碼
完全自定義的頭-header
- 編輯已有Header中的元素
- 新增Header中請求時,需要的Key:Value
- 禁用及啟用Header中的元素,對于不清楚API請求時,具體哪些是
必須
要傳遞的比較有用
完美的數據體-body
- 在請求需要發送數據體時,可通過設置
body
內容 - 支持form-data/x-www-urlencoded/raw/binary,格式的數據
請求前還能做點事-Pre-request Script
- 請求發送前,可進行一些腳本設置。如:設置或清除參數、變量
驗證API請求結果-Tests
Postman
提供了常用的測試功能:返回內容處理、狀態碼判斷、請求超時等。點擊后自動
添加到Test腳本中
進階使用
帶你快速走向各個環境-Environment
- 添加
QA環境
的URL
地址變量
添加QA環境的地址變量 - 添加
DEV
環境的URL
地址變量。此時就有兩個環境地址
QA、DEV,兩個環境 - 修改請求中的
URL
地址為:{{URL}}p/4a677c5f79f1
,切換環境至QA環境
變更URL、切換環境 - 點擊
Send
,請求QA環境
中的數據
真實的請求QA環境
使用Evnironment,可設置環境地址及環境中的不同數據,便于在跨環境后, 相同API可快速使用,減少調整API的成本
分享你的成果-導出/導入
or share
導出-Download
- Postman中有
Download
功能(即導出
功能),且Download
后的可直接導入
- 可將
Collection
、Environment
的數據進行Download
PS:導出的數據其實是JSON數據格式
,可隨意玩
導入-Import
- 導入
所有導出
的數據 - 被導入的數據格式與內容與導出時,完全一致。
分享-share
-
share
功能需要登錄后,才能使用。且shared成功后,對應的鏈接會保存到用戶數據中
share-link - 點擊鏈接,會直接打開share的API設置內容
導出/導入、share,便于在團隊內部協作時使用,API的請求及管理團隊化
海量執行你的請求 - Runner
當你有大量
的API時,肯定想一次執行多個
、多次執行多個
,Postman的Runner
可以滿足你的需求。
- 調整API的結構、添加必要的
測試驗證
調整 - 打開
Runner
,設置執行參數
:選擇要執行的Collection
、執行多少次Interation
、請求的延遲Delay
、數據文件、變量控件
執行參數 -
Start Test
查看運行結果RESULTS
Results
CI掛起來 - Newman
直接使用Newman
命令,快速把Postman與CI集成起來(直接使用shell命令,即可)。
newman -c demo.postman_collection --exitCode 1
PS:
- 從3.2.0版本之后,將原來的Jetpack(Runner/Newman)功能免費,以上所提到的功能
全部免費
使用 - 本文使用版本為4.2.2
參考
- Postman官網
http://getpostman.com/ - Newman
https://www.npmjs.com/package/newman - Postman Collection
http://www.getpostman.com/docs/collections - Postman Environment
https://www.getpostman.com/docs/test_multi_environments - How to write powerful automated API tests with Postman, Newman and Jenkins
http://blog.getpostman.com/2015/09/03/how-to-write-powerful-automated-api-tests-with-postman-newman-and-jenkins/