HttpRunner 新增支持 Postman 用例轉換

Postman 是一款強大、實用且容易上手的接口測試工具,它支持將集合(Collection)導出爲 JSON 格式的工程文件,從而可以可以靈活地和其他工具進行協調交互。

HttpRunner 從 v4.1.0 開始內置支持了 Postman Collection 的用例轉換功能。本文將結合具體案例來介紹該功能,如果大家在使用過程中遇到任何問題,歡迎在下方評論區留言,我們將根據大家的反饋來進行迭代和優化。

Postman 工程文件導出

首先,我們基於一個實際的 Postman 項目案例來導出 Postman 的工程文件。在 Postman 的 Explore 界面中可以找到非常多的項目案例,這裏我們選擇一個 Postman 官方提供的工作空間(Workspace)Published Postman Templates,打開該工作空間後可以看到官方提供了非常多的集合,這裏我們選擇其中的一個集合 Postman Echo 爲例,postman-echo 是一個用來調試 REST 客戶端發送請求功能是否正常的服務,具體可以參考 postman-echo 的接口文檔。

Postman Echo 集合的詳情頁面如下圖所示,通過 Create a fork 選項,我們可以將集合複製到自己的工作空間,從而可以進行其他自定義修改。這裏我們選擇直接通過 Export 選項來將 Postman Echo 集合導出爲 JSON 格式的工程文件,具體操作方式如下圖所示:

在隨後彈出的導出選項中,我們選擇最新版本 Collection v2.1,點擊 Export 將 Postman 工程文件導出到本地。

讀者可以按照上述的操作流程自行導出 Postman 工程文件到本地,此處筆者分享下自己導出的工程文件 postman_collection.json。從 JSON 工程文件的形式上可以看出,Postman 導出的工程文件與 HttpRunner 的測試用例有相似之處,都是採用了結構化的方式構建請求樣例,不過相比較而言,Postman 的工程文件結構更爲複雜。關於該工程文件的詳細格式說明和其他細節,感興趣的讀者可以參考 Collection 格式的介紹文檔,下面我們來演示從 Postman 到 HttpRunner 的用例轉換過程。

Postman 用例轉換

使用過 HttpRunner 的讀者可能對 hrp har2case 指令不陌生,不過隨着後續支持的輸入類型越來越多,HttpRunner 的轉換功能將不僅僅侷限於支持 Postman、Swagger 等工具導出的工程文件到 HttpRunner 測試用例的轉換,還會擴展支持 curl、Apach ab 等指令到測試用例的轉換,甚至是會支持測試用例的不同形態之間的相互轉換,橫向的拓展勢必會導致指令冗餘以及用戶的學習成本增加。因此,HttpRunner 之後的用例轉換功能將集中在 hrp convert 一個指令中,不過仍然兼容之前版本中 hrp har2case 的操作。與 hrp har2case 類似,通過 hrp convert 可以一次性轉換一或多個 Postman 工程文件,單個 Postman 工程文件經過轉換會生成對應的單個 JSON/YAML 腳本。下面介紹如何使用 hrp convert 指令來進行 Postman 用例轉換。

首先介紹 hrp convert 各個選項的詳細說明:

  1. --to-json/--to-yaml/--to-gotest/--to-pytest 用於將輸入轉化爲對應形態的測試用例,四個選項中最多隻能指定一個,如果不指定則默認會將輸入轉化爲 JSON 形態的測試用例。當前已經支持將 Postman 用例轉換爲 JSON/YAML/pytest 三種形態的測試用例

  2. --output-dir 後接測試用例的期望輸出目錄的路徑,用於將轉換生成的測試用例輸出到對應的文件夾,需要注意該路徑必須是存在且合法的

  3. --profile 後接 profile 配置文件的路徑,可實現對 HTTP 請求的 Headers/Cookies 進行替換或覆蓋。下面給出這兩種修改模式的 profile 配置文件示例:

headers:
  Header1: "this header will be created or updated"
cookies:
  Cookie1: "this cookie will be created or updated"
override: true
headers:
  Header1: "all original headers will be overridden"
cookies:
  Cookie1: "all original cookies will be overridden"

下面執行 hrp convert 指令來進行 Postman 用例轉換。在前面的 Postman 工程文件導出步驟中,我們得到了文件 postman_collection.json,在該文件所在的目錄打開終端並執行 hrp convert postman_collection.json --to-yaml 即可實現轉換過程,此處以轉化爲 YAML 測試用例爲例,執行結果如下:

$ hrp convert postman_collection.json --to-yaml                 
11:53AM INF Set log to color console other than JSON format.
11:53AM ??? Set log level
11:53AM INF load file path=postman_collection.json
11:53AM INF load file path=postman_collection.json
11:53AM INF load case as: postman input path=postman_collection.json
11:53AM INF start converting input path=postman_collection.json
11:53AM INF convert teststep method=GET url=https://postman-echo.com/get?foo1=bar1&foo2=bar2
11:53AM INF convert teststep method=POST url=https://postman-echo.com/post
11:53AM INF convert teststep method=POST url=https://postman-echo.com/post
11:53AM INF convert teststep method=PUT url=https://postman-echo.com/put
11:53AM INF convert teststep method=PATCH url=https://postman-echo.com/patch
11:53AM INF convert teststep method=DELETE url=https://postman-echo.com/delete
11:53AM INF convert teststep method=GET url=https://postman-echo.com/headers
11:53AM INF convert teststep method=GET url=https://postman-echo.com/response-headers?foo1=bar1&foo2=bar2
11:53AM INF convert teststep method=GET url=https://postman-echo.com/basic-auth
11:53AM INF convert teststep method=GET url=https://postman-echo.com/digest-auth
11:53AM INF convert teststep method=GET url=https://postman-echo.com/auth/hawk
11:53AM INF convert teststep method=GET url=https://postman-echo.com/oauth1
11:53AM INF convert teststep method=GET url=https://postman-echo.com/cookies/set?foo1=bar1&foo2=bar2
11:53AM INF convert teststep method=GET url=https://postman-echo.com/cookies
11:53AM INF convert teststep method=GET url=https://postman-echo.com/cookies/delete?foo1&foo2
11:53AM INF convert teststep method=GET url=https://postman-echo.com/status/200
11:53AM INF convert teststep method=GET url=https://postman-echo.com/stream/5
11:53AM INF convert teststep method=GET url=https://postman-echo.com/delay/2
11:53AM INF convert teststep method=GET url=https://postman-echo.com/encoding/utf8
11:53AM INF convert teststep method=GET url=https://postman-echo.com/gzip
11:53AM INF convert teststep method=GET url=https://postman-echo.com/deflate
11:53AM INF convert teststep method=GET url=https://postman-echo.com/ip
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/now
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/valid?timestamp=2016-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/format?timestamp=2016-10-10&format=mm
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/unit?timestamp=2016-10-10&unit=day
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/add?timestamp=2016-10-10&years=100
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/subtract?timestamp=2016-10-10&years=50
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/start?timestamp=2016-10-10&unit=month
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/object?timestamp=2016-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/before?timestamp=2016-10-10&target=2017-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/after?timestamp=2016-10-10&target=2017-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/between?timestamp=2016-10-10&start=2017-10-10&end=2019-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/time/leap?timestamp=2016-10-10
11:53AM INF convert teststep method=GET url=https://postman-echo.com/digest-auth
11:53AM INF dump data to yaml path=/Users/bytedance/code/go/httprunner/postman_collection_test.yaml
11:53AM INF conversion completed output files=["postman_collection_test.yaml"]

在執行了上述操作後,可以得到轉換生成的 HttpRunner YAML 格式的測試用例,之後可以使用 hrp run 和 hrp boom 基於該測試用例來進行後續的接口 / 性能測試。同理,我們也可以指定 --to-pytest 選項來將輸入轉化爲 pytest 腳本形態的測試用例。

注意事項

  1. hrp convert 可以自動識別輸入類型,因此不需要通過選項來手動制定輸入類型,如遇到無法識別、不支持或轉換失敗的情況,則會輸出錯誤日誌並跳過,不會影響其他轉換過程的正常進行

  2. 輸出的測試用例文件名格式爲 Postman 工程文件名稱(不帶拓展名) + _test + .json/.yaml/.go/.py 後綴 ,如果該文件已經存在則會進行覆蓋

  3. 在 profile 配置文件中,指定 override 字段爲 false/true 可以選擇修改模式爲替換 / 覆蓋,需要注意的是,如果不指定該字段則默認修改模式爲替換模式

  4. 如果 Postman 工程文件中包含了文件夾層級結構,則 hrp convert 會將層級結構遞歸展開,所有請求按順序轉換生成 HttpRunner 測試用例中的若干步驟,步驟名稱爲該請求在原始 Postman 工程文件中的路徑,路徑中的層級之間用連字符 “-” 分隔,從而可以有效幫助用戶定位到各個請求在原始 Postman 工程文件中的位置

  5. 完整支持 Postman 請求 Params 中的請求參數變量、請求路徑變量機制,hrp convert 會自動解析各個變量名並進行替換

  6. 完整支持 Postman 請求 Body 中的 form-data/x-www-form-urlencoded/raw 類型,不過暫不支持 binary / GraphQL 類型

What's next

  1. 支持轉換 Postman 請求 Authorization 中的內容,補充測試用例 Headers 信息

  2. 支持轉換 Postman 請求 Pre-request Script 中的內容爲 Setup 操作

  3. 支持轉換 Postman 請求 Tests 中的內容爲請求結果斷言操作

高能預告!!!

HttpRunner 除了支持 Postman 用例轉換外,還會支持 Swagger / JMeter / curl / ab 等衆多 API 工具格式的導入功能,詳細規劃將在下一期文章《HttpRunner v4.0 用例轉換功能規劃》中介紹,敬請期待~

本文作者:卜卜星(HttpRunner 核心開發者)
HttpRunner 項目官網: https://httprunner.com/
如果 HttpRunner 對你有過幫助,麻煩幫忙給個 ⭐️star⭐️ 鼓勵下吧
https://github.com/httprunner/httprunner

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/CjucRX0aXQbHNX57v11eqA