postman變數的使用和設定

2022-03-14 20:28:27 字數 3667 閱讀 5833

之前只是使用postman做介面管理——將各個專案使用到的介面分類管理起來,用的時候手動改引數呼叫。這次專案連著跑三個介面,需要用到前乙個介面的引數,還來回切平台,真的很麻煩,所以就搜了一下有什麼便利的方式沒有。

1、新增乙個環境,右上角眼睛或者左邊new選單欄裡的environment

或者

2、在新增環境介面設定環境名和環境裡的變數,可以給變數初始值,也可以不給(可以後續走介面返回值設定該變數的值)

3、變數的使用:通過形式:},當切換環境的時候(右側環境名箭頭下拉,有你儲存的所有環境,可選擇當前環境),這個變數則為不同的值。所以通過切換環境,我們可以批量改變乙個請求中的多個引數

方式1:

上文中的手動輸入設定

方式二:

**設定(對全域性變數,全域性變數針對所有環境有效)

編輯器旁邊列出常用的**段來輔助寫

tests

1. 清除乙個全域性變數

clear a global variable

對應指令碼:

postman.clearglobalvariable("variable_key");

引數:需要清除的變數的key

2.清除乙個環境變數

clear an environment variable

對應指令碼:

postman.clearenvironmentvariable("variable_key");

引數:需要清除的環境變數的key

3.response包含內容

response body:contains string

對應指令碼:

tests["body matches string"] =responsebody.has("string_you_want_to_search");

引數:預期內容

4.將xml格式的response轉換成son格式

response body:convert xml body to a json object

對應指令碼:

var jsonobject =xml2json(responsebody);

引數:(預設不需要設定引數,為介面的response)需要轉換的xml

5.response等於預期內容

response body:is equal to a string

對應指令碼:

tests["body is correct"] = responsebody === "response_body_string";

引數:預期response

6.json解析key的值進行校驗

response body:json value check

對應指令碼:

tests["args key contains argument passed as url parameter"] = 'test' in

responsejson.args

引數:test替換被測的值,args替換被測的key

7.檢查response的header資訊是否有被測字段

response headers:content-type header check

對應指令碼:

tests["content-type is present"] = postman.getresponseheader("content-type");

引數:預期header

8.響應時間判斷

response time is less than 200ms

對應指令碼:

tests["response time is less than 200ms"] = responsetime < 200;

引數:響應時間

9.設定全域性變數

set an global variable

對應指令碼:

postman.setglobalvariable("variable_key", "variable_value");

引數:全域性變數的鍵值

10.設定環境變數

set an environment variable

對應指令碼:

postman.setenvironmentvariable("variable_key", "variable_value");

引數:環境變數的鍵值

11.判斷狀態碼

status code:code is 200對應指令碼:

tests["status code is 200"] = responsecode.code != 400;

引數:狀態碼

12.檢查code name 是否包含內容

status code:code name has string

對應指令碼:

tests["status code name has string"] = responsecode.name.has("created");

引數:預期code name包含字串

13.成功的post請求

status code:successful post request

對應指令碼:

tests["successful post request"] = responsecode.code === 201 || responsecode.code === 202;

14.微小驗證器

use tiny validator

forjson data

對應指令碼:

var schema =

};var data1 = [true, false

];

var data2 = [true, 123];

console.log(tv4.error);

tests["valid data1"] =tv4.validate(data1, schema);

tests["valid data2"] =tv4.validate(data2, schema);

引數:可以修改items裡面的鍵值對來對應驗證json的引數

Postman變數的設定方式

postman 中的變數有 環境變數 對介面測試的環境設定,一般用來設定介面的ip和埠號 全域性變數 作用域的範圍為整個postman,一般用在多個介面用例引用同乙個變數時 區域性變數 作用到摸個測試集中的變數 變數的設定方式 第一種進入變數設定介面,輸入key值和value值 介面通過 引用變數 ...

Postman 設定環境變數

1 環境變數的作用域 使用環境變數 可用於切換開發環境 生產環境 設定動態引數 有4個作用域 優先順序由高到低 global,environment,local,data。同名變數會使用優先順序高的。環境變數可以在請求的url 引數 header script中訪問到。作用域可以被視為值駐留的不同種...

postman如何設定環境變數和全域性變數

1.使用postman測試介面時,不同的環境如何設定環境變數呢?1 開啟postman,點選右上角的設定按鈕,選擇manage environments 2 點選add,新增環境變數 3 輸入環境名稱,變數名和變數值 4 新增成功後,回到主頁調介面時,右上角選中剛剛新增的環境名稱,即可引用該環境變數...