一文搞定 Postman 接口自動化測試

本文適合已經(jīng)掌握 Postman 基本用法的讀者,即對接口相關(guān)概念有一定了解、已經(jīng)會使用 Postman 進行模擬請求等基本操作绘沉。

工作環(huán)境與版本:

  • Window 7(64位)
  • Postman (Chrome App v5.5.3)

P.S. 不同版本頁面 UI 和部分功能位置會有點不同惰匙,不過影響不大铛铁。

首先我們來思考一下,如果要達到自動化接口測試效果崎溃,在基本的模擬請求上還需要做哪些呢蜻直?

我粗略概括為 3 個問題(歡迎在評論區(qū)留言更多補充建議):

1. 如何判斷接口是否請求成功?
2. 如何進行接口批量笨奠、定期測試袭蝗?
3. 如何處理依賴接口問題(比如商品下單的接口必須要求先登錄)唤殴?

所以般婆,接下來就主要分為 3 個部分,介紹如何解決這 3 個問題朵逝。

1 接口結(jié)果判斷

首先蔚袍,既然是自動化測試,那么我們肯定需要通過工具 Postman 或者代碼,幫我們直接判斷結(jié)果是否符合預期啤咽。那么在接口測試上晋辆,大體就兩個思路:

1. 判斷請求返回的 code 是否符合預期

2. 判斷請求返回的內(nèi)容中是否包含預期的內(nèi)容(關(guān)鍵字)

接下來我們看看如何利用 Postman 來解決上述的問題:

1.1 功能區(qū)

一文搞定 Postman 接口自動化測試

在 Postman 中相關(guān)的功能在非常顯眼的地方,Tests 功能的使用需要我們有一定的編程語言基礎宇整,目前支持的腳本語言即為 JavaScript 瓶佳。但比較好的一點是,我們不需要再去考慮上下文問題以及運行環(huán)境的問題 鳞青,也就是說我們只需要在這邊完成結(jié)果邏輯判斷的代碼塊即可霸饲。

而 Postman 還為我們提供了一些常用的代碼模板,在 Tests 面板右邊的 SNIPPETS 功能區(qū)中臂拓,所以對 JavaScript 不大了解問題也不大厚脉。代碼編寫相關(guān)將在下文進行具體介紹。

1.2 腳本相關(guān)

先看上圖的代碼部分胶惰,我們可以發(fā)現(xiàn) responseCode 傻工、 responseBody 和 tests 三個變量(可直接使用) :

  • responseCode :包含請求的返回的狀態(tài)信息(如:code)。
  • responseBody:為接口請求放回的數(shù)據(jù)內(nèi)容(類型為字符串)孵滞。
  • tests :為鍵值對形式中捆,用于表示我們的測試結(jié)果是成功與否,最終展示在 Test Results中坊饶。
  • key :(如:code 200)我們可以用來當做結(jié)果的一個描述轨香。
  • value:其值為布爾型,ture 表示測試通過幼东, false 表示測試失敗臂容。

所以上述代碼應該不難理解了,而有了返回結(jié)果的數(shù)據(jù)以及表示結(jié)果成功與否的方式根蟹,那么我們“接口結(jié)果判斷”的問題也就基本解決了脓杉。

另外還有幾個比較常用的:

  • responseTime :請求所耗時長

  • postman :可以做的比較多,比如

  • 獲取返回數(shù)據(jù)的頭部信息:
    postman.getResponseHeader("")

  • 設置全局變量:
    postman.setGlobalVariable("variable_key", "variable_value");

更多功能可以查看官方文檔简逮。

1.3 代碼模板

Postman 在 SNIPPETS 功能區(qū)中為我們提供的代碼模板已經(jīng)能解決大部分情況了球散,以下先挑幾個跟結(jié)果判斷相關(guān)的進行講解:

Status code : Code is 200

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//根據(jù)返回的 Code 判斷請求情況
tests["Status code is 200"] = responseCode.code === 200;
</pre>

Response body: Contains string

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//判斷返回的內(nèi)容中是否存在“關(guān)鍵字”。(tests 的 key 可修改散庶,將不再強調(diào))
tests["Body matches string"] = responseBody.has("這里可以改為你要判斷的關(guān)鍵字內(nèi)容");
//如上文提到的:
//判斷結(jié)果中是否存在 access_token 關(guān)鍵字
tests["has access_token"] = responseBody.has("access_token");
</pre>

Response body: is equal to string

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//判斷返回內(nèi)容是否跟預期完全相等蕉堰。
tests["Body is correct"] = responseBody === "這里可以改為你的預期內(nèi)容";
</pre>

Response body: JSON value check

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//上文提到,responseBody 為字符串類型悲龟,支持轉(zhuǎn)為 Json 格式
var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value === 100;
</pre>

Response time is less than 200ms

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//判斷請求時長是否小于200ms ,具體時長按情況自定義
tests["Response time is less than 200ms"] = responseTime < 200;
</pre>

以上介紹的這些基本已經(jīng)足夠完成對單一接口的測試了屋讶,但我們知道如果沒有批量、定時任務, 那么這些都將毫無意義须教,所以繼續(xù) …

2 集合(批量)測試

想要進行接口的批量測試皿渗、管理斩芭,那么我們需要將待測試的接口全部都保存到同一個集合(Collections)中,你可以認為就是保存到同一個文件夾中乐疆。先看看 Postman 中的操作步驟:

一文搞定 Postman 接口自動化測試

通過以上步驟划乖,我們得到一個待測的接口集合,為了簡化情況挤土,我這邊每個接口成功與否的條件都是用 code 是否為 200 來判斷:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">tests["Status code is 200"] = responseCode.code === 200;
</pre>

2.1 批量執(zhí)行

以上準備就緒后琴庵,我們就可以開始批量運行接口進行測試了:

一文搞定 Postman 接口自動化測試

點擊Run 后,會新打開一個頁面:

一文搞定 Postman 接口自動化測試
  • Environment :用于切換接口運行的環(huán)境仰美,這里先不管细卧,后面再講
  • Iteration :用于設置接口一共要運行的次數(shù)。
  • Delay : 設置每次運行接口之間的時間間隔筒占,單位為毫秒贪庙。
  • Data File : 上傳測試數(shù)據(jù)文件 (下文單獨講)

2.2 變化的參數(shù)數(shù)據(jù)

我們已經(jīng)了解了,如何讓多個接口循環(huán)運行多次翰苫,但是現(xiàn)在有個問題止邮,按目前這個步驟,每次運行時接口的參數(shù)都是一樣的奏窑,那么就算我們運行個100次导披、1000次意義也不大。

先看看我們寫好的一個登錄功能的接口:

一文搞定 Postman 接口自動化測試

使用變量

現(xiàn)在登錄的賬號和密碼參數(shù)都是寫死的埃唯,也就是不過我們執(zhí)行多少次撩匕,都是拿這個賬號去測試。那么如果想要測試賬號密碼參數(shù)使用其它值有沒有異常怎么辦呢墨叛?( 想要每次都手動改的可以跳過這部分 /手動滑稽)這里我們先簡單講一下在 Postman 中使用如何“變量”止毕,如下圖:

一文搞定 Postman 接口自動化測試

引用一個變量的語法:{{變量名}}, 圖中可以看到漠趁,我們將賬戶和密碼字段的參數(shù)值都設置為變量:{{username}} 扁凛、{{password}} 。修改完直接點擊運行 (Send) 當然是不行的闯传,因為目前這兩個變量還未被賦值谨朝,不過我們可以在 Pre-request Script 面板中進行賦值操作:

Pre-request Script

Pre-request Script 與 Tests 類似,區(qū)別在于:Pre-request Script 中的腳本是在執(zhí)行請求之前運行甥绿,而Tests 中的腳本則是在請求完成之后執(zhí)行字币。所以,我們可以在 Pre-request Script 功能區(qū)中用腳本先個上面兩個變量進行賦值共缕,如:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//設置全局變量
postman.setGlobalVariable("username", "test1");
postman.setGlobalVariable("password", "123456");
</pre>

但是用 Pre-request Script 進行賦值操作仍然不能解決我們的問題洗出,因為按照這種寫法,不論運行多少次其實都還是用固定(寫死)的數(shù)據(jù)進行測試骄呼。當然既然是腳本語言共苛,也會有更靈活的用法,這邊先不將蜓萄。

測試數(shù)據(jù)集

接下來我們講講 Data File , 在運行集合前的這個選項就是用來上傳測試數(shù)據(jù)(文件)以賦值給相應變量的隅茎。我們先以 CSV 格式的測試數(shù)據(jù)為例:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> username,password
test1,123456
test2,222222
test3,123456
test4,444444
</pre>

數(shù)據(jù)格式類似表格,第一行表示對應的變量名嫉沽,下面 4 行表示 4 組賬號密碼數(shù)據(jù)(其中兩組為正確數(shù)據(jù)) 辟犀,我們保存一份內(nèi)容為上述示例數(shù)據(jù)后綴名為.csv 的文件后,再次開始測試看看效果绸硕,我們選擇運行次數(shù)為 4 (對應 4 組測試數(shù)據(jù))堂竟、選擇對應的 CSV 文件運行后,可以看到我們的結(jié)果確實如我們的預期玻佩。接口 Request 運行的結(jié)果為兩次成功兩次失敗出嘹,也就是每一次運行都賦值了不同的賬號密碼的測試數(shù)據(jù) (在最新的桌面客戶端版本中可以看到每次具體的請求情況,這邊就不再細說了)咬崔。

如果使用 Json 文件的話税稼,那么格式如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> [
{
"username": "test1",
"password": "123456"
},
{
"username": "test2",
"password": "222222"
},
{
"username": "test3",
"password": "123456"
},
{
"username": "test4",
"password": "444444"
}
]
</pre>

2.3 定期任務

Postman 提供了一個 Monitors (監(jiān)視器)功能,支持我們提交一個測試任務垮斯,按照設置的定時器進行運行郎仆,如每小時測試一次,具體操作如下:

一文搞定 Postman 接口自動化測試

3 請求依賴問題

講完接口結(jié)果判斷和集合批量測試后兜蠕,我們再來看看比較復雜的情況扰肌,即依賴請求問題,比如我們的購物下訂單接口要求必須先登錄后才可訪問熊杨。但大部分依賴問題其實本質(zhì)上就是一個接口間數(shù)據(jù)傳遞的問題曙旭,比如調(diào)用登錄接口后返回一個標識,假設為 token 晶府,那么我們請求下訂單接口時只要一起攜帶 token 參數(shù)進行請求即可夷狰。所以,問題變?yōu)椋?/p>

  • 保證接口調(diào)用順序
  • 將接口A返回的數(shù)據(jù)傳遞給后續(xù)的接口B郊霎、C沼头、D

3.1 接口執(zhí)行順序

首先,說明一下书劝,接下來說的接口都是默認屬于同一個集合 (Collections) 中的进倍。

還是以我們上文中創(chuàng)建好接口集合為例,如果你有注意我們執(zhí)行批量測試的結(jié)果购对,就會發(fā)現(xiàn)接口的執(zhí)行順序其實就是按照這邊目錄中的順序(從上到下)猾昆,即: Request1 -> Request2 -> Request3。

一文搞定 Postman 接口自動化測試

這邊接口名字可能有點誤導性骡苞,所以再強調(diào)一下:按目錄中從上到下的順序執(zhí)行 (與字典排序無關(guān))

所以有了這個默認的執(zhí)行順序后垂蜗,那么我們便可以把需要優(yōu)先執(zhí)行的接口放前面即可楷扬,比如把“登錄接口”放在第一個。

3.1.2 自定義執(zhí)行順序

當然贴见,如果只有默認的一個執(zhí)行順序的話烘苹,通常沒法滿足我們復雜的業(yè)務需求,所以 Postman 為我們提供了一個函數(shù):postman.setNextRequest("填寫你要跳轉(zhuǎn)的接口名") 片部,支持我們跳轉(zhuǎn)到指定接口繼續(xù)執(zhí)行镣衡,舉個例子:

我們在運行完 Request1 接口成功后,不需要再運行 Request2 而是直接跳至 Request3 档悠,那么我可以在 Request1 接口的 Tests 功能區(qū)中執(zhí)行跳轉(zhuǎn)代碼廊鸥,如:

一文搞定 Postman 接口自動化測試

這里需要注意幾點:

1. postman.setNextRequest() 只在運行集合測試的時候生效,也就是說我們單獨運行 (Send)接口Request1 時辖所,函數(shù)是不起作用的惰说。
2. 當我們運行集合測試成功從 Request1 -> Request3 后,如果 Request3 后面還有接口缘回,那么后面的接口仍然繼續(xù)按默認順序執(zhí)行助被,即圖中的接口 Request4 仍會被執(zhí)行。
3. 指定的跳轉(zhuǎn)接口必須屬于同一個集合中切诀。
4. setNextRequest() 函數(shù)不管在 Tests 腳本中何處被調(diào)用揩环,它都只在當前腳本最后才被真正執(zhí)行。比如我們將圖中的第二行與第一行互調(diào)后幅虑,那么在運行跳轉(zhuǎn)函數(shù)后第二行代碼仍會被執(zhí)行丰滑。

所以,利用 setNextRequest() 函數(shù)倒庵,我們便可以按照條件跳過不必要的接口褒墨,或者建立我們自己的一個邏輯測試。

3.2 數(shù)據(jù)傳遞

在講數(shù)據(jù)傳遞前擎宝,先聊聊 Postman 中全局變量郁妈、環(huán)境切換的使用。

3.2.1 全局變量

全局變量的概念其實我們在上文中講 Pre-request Script 時有簡單提到绍申,也就是說我們可以通過腳本代碼來設置全局變量噩咪,我們可以看看運行上文的腳本后的效果:

我們可以看到運行后,username 和 password 兩個變量已經(jīng)被成功保存下來极阅,那么我們在任意接口中便都可以通過變量引用的語法如:{{username}} 來使用它們胃碾。

另外,Postman 不僅支持代碼設置全局變量的方式筋搏,它還支持可視化操作:

一文搞定 Postman 接口自動化測試

進入對應界面后仆百,便可直接進行管理:

一文搞定 Postman 接口自動化測試

3.2.2 多環(huán)境區(qū)分與切換

通常情況下,我們的接口都會分為測試版本和線上版本(或者更多)奔脐,而他們的區(qū)別可能僅是 ULR 不同俄周,那么全局變量便不大合適解決這個問題吁讨。

參數(shù)的創(chuàng)建

可能你已經(jīng)注意到,上圖中我已經(jīng)建有幾個不同環(huán)境的參數(shù)“集合”了峦朗,再看一下:

一文搞定 Postman 接口自動化測試

我在每個環(huán)境中都創(chuàng)建了一個 host 參數(shù)建丧,如:

一文搞定 Postman 接口自動化測試

當然,我們的環(huán)境參數(shù)也可以通過腳本的方式來進行設置甚垦,函數(shù)為:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//注意茶鹃,該參數(shù)只添加到你當前選擇的環(huán)境的“參數(shù)集”中
postman.setEnvironmentVariable("variable_key", "variable_value");</pre>

使用與切換

環(huán)境“參數(shù)集” 中的參數(shù)使用方式和全局變量一致涣雕,如圖中 {{host}} 艰亮,不同環(huán)境的切換見下圖:

一文搞定 Postman 接口自動化測試

3.3 解決依賴問題

掌握以上的預備知識后,我們開始看看如何用 Postman 解決存在依賴關(guān)系的接口測試挣郭。

假設場景

我們的接口 Request1 為登錄接口迄埃,登錄成功將會返回一個 access_token 字段作為標識(已實現(xiàn))。那么假設接口 Request3 為一個下訂單的接口兑障,需要攜帶登錄返回的 access_token 才能正常訪問侄非。

思路

1. 保證 Request1 在 Request3 之前被運行
2. 將 Request1 返回的 access_token 的值添加到環(huán)境變量"參數(shù)集"中。
3. Request3 在請求時引用 access_token 的值

將返回值存在 “全局變量” 或者 “環(huán)境變量” 中流译,視具體業(yè)務情況而定逞怨,該例中 access_token 的值是與環(huán)境有關(guān)的,所以這里選擇使用環(huán)境變量集存儲福澡。

Postman 中的操作

1. 我們目錄中已保證 Request1 接口優(yōu)先執(zhí)行
2. Request1 中 Tests 的代碼情況:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if(responseCode.code === 200 && responseBody.has("access_token")){
//如果 code 為 200叠赦, 并且返回的數(shù)據(jù)中存在 access_token 關(guān)鍵字,則認為登錄成功
tests["login"] = true;

//將返回的內(nèi)容轉(zhuǎn)為 json 格式革砸,并且取到 access_token 內(nèi)容除秀,添加到環(huán)境變量中
var jsonData = JSON.parse(responseBody);
//access_token的取值方式視具體的 json 數(shù)據(jù)結(jié)構(gòu)而定
postman.setEnvironmentVariable("token",jsonData.result.access_token);

//跳轉(zhuǎn)到 Request3 接口
postman.setNextRequest("Request3")

}else{
tests["login"] = false;

//登錄失敗,可以選擇跳轉(zhuǎn)到對應失敗后的處理接口進行測試
//postman.setNextRequest("Other Request")
}</pre>

3. 在接口 Request3 中使用變量 token :

一文搞定 Postman 接口自動化測試

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">> 我這邊是將 token 放在頭部信息中算利, 具體使用方式時接口參數(shù)規(guī)則而定册踩。</pre>

運行并查看結(jié)果

運行集合測試,可以看到我們結(jié)果符合我們的預期效拭,Request1 和 Request3 通過測試暂吉,Request2 被跳過,Request4 仍被執(zhí)行缎患。Done…

原文鏈接:
https://blog.csdn.net/cai_iac/article/details/81030619

(霍格沃茲測試學院--測試工程師的黃埔軍校)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末借笙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子较锡,更是在濱河造成了極大的恐慌业稼,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚂蕴,死亡現(xiàn)場離奇詭異低散,居然都是意外死亡俯邓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進店門熔号,熙熙樓的掌柜王于貴愁眉苦臉地迎上來稽鞭,“玉大人,你說我怎么就攤上這事引镊‰蹋” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵弟头,是天一觀的道長吩抓。 經(jīng)常有香客問我,道長赴恨,這世上最難降的妖魔是什么疹娶? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮伦连,結(jié)果婚禮上雨饺,老公的妹妹穿的比我還像新娘。我一直安慰自己惑淳,他們只是感情好额港,可當我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著歧焦,像睡著了一般移斩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上倚舀,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天叹哭,我揣著相機與錄音,去河邊找鬼痕貌。 笑死风罩,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的舵稠。 我是一名探鬼主播超升,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼哺徊!你這毒婦竟也來了室琢?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤落追,失蹤者是張志新(化名)和其女友劉穎盈滴,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡巢钓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年病苗,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片症汹。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡硫朦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出背镇,到底是詐尸還是另有隱情咬展,我是刑警寧澤,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布瞒斩,位于F島的核電站破婆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏济瓢。R本人自食惡果不足惜荠割,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一妹卿、第九天 我趴在偏房一處隱蔽的房頂上張望旺矾。 院中可真熱鬧,春花似錦夺克、人聲如沸箕宙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柬帕。三九已至,卻和暖如春狡门,著一層夾襖步出監(jiān)牢的瞬間陷寝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工其馏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凤跑,地道東北人。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓叛复,卻偏偏與公主長得像仔引,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子褐奥,可洞房花燭夜當晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容