Postman速查表


這份Postman速查表基於Postman官方文件頁面(連結如下)和對Postman的整體理解:

https://learning.postman.com/docs/getting-started/introduction/

a. 變數

所有變數都可以透過Postman的GUI手動設定,並且它們具有定義的範圍。變數的值也可以透過在“預請求指令碼”或“測試”選項卡下編寫的指令碼設定。

變數可以以{{<變數名>}}的格式新增到請求URL、Headers和Body中。

在請求URL中的用法:

https://{{domain}}about/{{id}}
Usage in request Headers(key-value):
X-{{key}}:value

在請求Body中的用法:

{"registration_id": "{{Id}}", "firstname": "Postman"}

b. 全域性變數

當我們需要向其他請求傳送資料時,使用全域性變數。新增全域性變數的指令碼可以包含在Postman的“測試”或“預請求指令碼”選項卡中。

設定全域性變數:

pm.globals.set('<name of Global variable>', '<value of variable>')

獲取全域性變數的值:

pm.globals.get('<name of Global variable>')

刪除全域性變數的指令碼:

pm.globals.unset('<name of Global variable>')

刪除所有全域性變數的指令碼:

pm.globals.clear()

c. 集合變數

集合變數是全域性變數和環境變數的一個很好的替代方案。如果只有一個環境,它們也可以用於URL/身份驗證憑據。新增集合變數的指令碼可以包含在Postman的“測試”或“預請求指令碼”選項卡中。

設定集合變數:

pm.CollectionVariables.set('<name of variable>', '<value of variable>')

獲取集合變數的值:

pm.CollectionVariables.get('<name of Collection variable>')

刪除集合變數的指令碼:

pm.CollectionVariables.unset('<name of Collection variable>')

d. 環境變數

環境變數用於特定環境。它們是全域性變數的一個很好的替代方案,因為它們的範圍有限。新增全域性變數的指令碼可以包含在Postman的“測試”或“預請求指令碼”選項卡中。

環境變數用於儲存特定於環境的變數、URL以及向其他請求傳送資料。

設定環境變數:

pm.environment.set('<name of Environment variable>', '<value of variable>')

獲取環境變數:

pm.environment.get('<name of Environment variable>')

刪除環境變數:

pm.environment.unset('<name of Environment variable>')

刪除所有環境變數的指令碼:

pm.environment.clear()

獲取活動環境名稱的指令碼:

pm.environment.name

e. 資料變數

資料變數用於在集合執行器或Newman中執行特定迭代。它們主要用於從CSV/JSON檔案中獲取多個數據集的情況。

獲取資料變數的值:

pm.iterationData.get('<name of Data variable>')

f. 本地變數

本地變數可以在請求內或透過集合執行器/Newman執行時訪問。這些變數在請求執行後預設會被刪除。

設定本地變數:

pm.variables.set('<name of Local variable>', '<value of variable>')

獲取本地變數:

pm.variables.get('<name of Local variable>')

g. 動態變數

動態變數可以與字串一起使用以生成動態和不同的資料。

JSON body中動態變數的示例:

{"email": "test.{{$timestamp}}@gmail.com"}

h. 除錯變數

啟動Postman控制檯,並在“預請求指令碼”或“測試”選項卡下的指令碼中新增console.log來除錯變數。

console.log(pm.globals.get('< name of Global variable >')

i. 斷言

斷言新增到帶有pm.test回撥的塊下。

pm.test("Response Status Code", function () {
pm.response.to.have.status(201)
})

j. 跳過測試

要跳過測試,我們必須新增pm.test.skip。跳過的測試在報告中可見。

pm.test.skip("Response Status Code", function () {
pm.response.to.have.status(201)
})

k. 測試失敗

在Postman中,我們可以顯式地使測試失敗,而無需新增斷言。

pm.expect.fail('Scenario is failed');

更新於:2021年6月25日

瀏覽量:1K+

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.