postman 變量-環境變量,集合變量(項目變量),全局變量,普通變量


 

postman 版本信息:

Postman for Windows
Version 7.14.0
win32 6.1.7601 / x64
 
postman 有四種變量:環境變量,集合變量(項目變量),全局變量,普通變量
 
一:環境變量:應對不同開發環境,如local,staging,production環境不同的請求地址。則定義一個base_url變量
 
示例:創建production環境,變量a:
創建設置:設置-》add環境變量

 

VARIABLE 變量  INITIAL VALUE 默認值  CURRENT VALUE 當前值

 

 選擇切換環境-》點擊眼睛查看當前環境變量值

 

 

二:集合變量(項目變量):在一個集合以及集合下面的目錄中的請求都可以使用。主要給某組請求使用。比如我們可以定義一個項目為一個collection,請求token變量定義為一個集合變量。

示例:定義一個變量測試集合,並添加集合變量ca。

創建設置:collections->add collections (-> edit )->variables

 

 

三:全局變量:整個postman中的請求都可以使用。

示例:創建全局變量ga。

創建設置:變量設置-》global-》變量添加

 

 

四:普通變量-在請求周期內某個階段定義,后續階段可以使用。比如在 Request  Pre-request Script中定義,則后續可以在Collection Test中使用

請求生命周期圖:

 

 

五:使用四種變量

  1 在請求中通過{{變量名使用}},比如url地址或者raw中調用。

  示例:http://httpbin.org/get?a={{a}}&ca={{ca}}&ga={{ga}}

 

 

 2 在 Pre-request Script和Tests腳本中使用

Pre-request Script 可以設置變量,並且會修改current value 值。

環境變量操作

pm.environment.get("variable_key");
pm.environment.set("variable_key", "variable_value");

集合變量操作

pm.collectionVariables.get("variable_key");
pm.collectionVariables.set("variable_key", "variable_value");

全局變量操作

pm.globals.get("variable_key");
pm.globals.set("variable_key", "variable_value");

普通變量操作

pm.variables.get("variable_key");
pm.variables.set("variable_key", "variable_value");

 

 

 

 

 

 3 Pre-request Script和Tests 執行次序 

collection Pre-request Script -》 folder Pre-request Script -》 本次request Pre-request Script ===》請求 Request -》響應 Response ===》collection Tests -》 folder Tests -》本次request Test 

 

參考:

https://learning.getpostman.com/docs/postman/scripts/intro-to-scripts/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM