接口自動化測試框架-httprunner V2.x中文使用手冊-介紹及安裝

介紹

HttpRunner 是一款面向 HTTP(S) 協議的通用測試框架,只需編寫維護一份 YAML/JSON 腳本,即可實現自動化測試、性能測試、線上監控、持續集成等多種測試需求。

此文檔適用於全新發布的 HttpRunner 2.x 版本,1.x 版本的使用文檔請查看歷史鏈接

設計理念

  • 充分複用優秀的開源項目,不追求重複造輪子,而是將強大的輪子組裝成戰車
  • 遵循 約定大於配置 的準則,在框架功能中融入自動化測試最佳工程實踐
  • 追求投入產出比,一份投入即可實現多種測試需求

核心特性

  • 繼承 Requests 的全部特性,輕鬆實現 HTTP(S) 的各種測試需求
  • 採用 YAML/JSON 的形式描述測試場景,保障測試用例描述的統一性和可維護性
  • 藉助輔助函數(debugtalk.py),在測試腳本中輕鬆實現複雜的動態計算邏輯
  • 支持完善的測試用例分層機制,充分實現測試用例的複用
  • 測試前後支持完善的 hook 機制
  • 響應結果支持豐富的校驗機制
  • 基於 HAR 實現接口錄製和用例生成功能(har2case
  • 結合 Locust 框架,無需額外的工作即可實現分佈式性能測試
  • 執行方式採用 CLI 調用,可與 Jenkins 等持續集成工具完美結合
  • 測試結果統計報告簡潔清晰,附帶詳盡統計信息和日誌記錄
  • 極強的可擴展性,輕鬆實現二次開發和 Web 平臺化

安裝說明

運行環境

HttpRunner 是一個基於 Python 開發的測試框架,可以運行在 macOS、Linux、Windows 系統平臺上。

Python 版本:HttpRunner 支持 Python 3.4 及以上的所有版本,並使用 Travis-CI 進行了持續集成測試,測試覆蓋的版本包括 2.7/3.4/3.5/3.6/3.7。雖然 HttpRunner 暫時保留了對 Python 2.7 的兼容支持,但強烈建議使用 Python 3.4 及以上版本。

操作系統:推薦使用 macOS/Linux。

安裝方式

HttpRunner 的穩定版本託管在 PyPI 上,可以使用 pip 進行安裝。

 

$ pip install httprunner

如果你需要使用最新的開發版本,那麼可以採用項目的 GitHub 倉庫地址進行安裝:

 

$ pip install git+https://github.com/HttpRunner/HttpRunner.git@master

版本升級

假如你之前已經安裝過了 HttpRunner,現在需要升級到最新版本,那麼你可以使用-U參數。該參數對以上三種安裝方式均生效。

 

$ pip install -U HttpRunner
$ pip install -U git+https://github.com/HttpRunner/HttpRunner.git@master

安裝校驗

在 HttpRunner 安裝成功後,系統中會新增如下 5 個命令:

  • httprunner: 核心命令
  • ate: 曾經用過的命令(當時框架名稱爲 ApiTestEngine),功能與 httprunner 完全相同
  • hrun: httprunner 的縮寫,功能與 httprunner 完全相同
  • locusts: 基於 Locust 實現性能測試
  • har2case: 輔助工具,可將標準通用的 HAR 格式(HTTP Archive)轉換爲YAML/JSON格式的測試用例

httprunner、hrun、ate 三個命令完全等價,功能特性完全相同,個人推薦使用hrun命令。

運行如下命令,若正常顯示版本號,則說明 HttpRunner 安裝成功。

 

$ hrun -V
2.0.2

$ har2case -V
0.2.0

開發者模式

默認情況下,安裝 HttpRunner 的時候只會安裝運行 HttpRunner 的必要依賴庫。

如果你不僅僅是使用 HttpRunner,還需要對 HttpRunner 進行開發調試(debug),那麼就需要進行如下操作。

HttpRunner 使用 pipenv 對依賴包進行管理,若你還沒有安裝 pipenv,需要先執行如下命令進行按照:

 

$ pip install pipenv

獲取 HttpRunner 源碼:

 

$ git clone https://github.com/HttpRunner/HttpRunner.git

進入倉庫目錄,安裝所有依賴:

 

$ pipenv install --dev

運行單元測試,若測試全部通過,則說明環境正常。

 

$ pipenv run python -m unittest discover

查看 HttpRunner 的依賴情況:

 

$ pipenv graph

HttpRunner==2.0.0
  - colorama [required: Any, installed: 0.4.0]
  - colorlog [required: Any, installed: 3.1.4]
  - har2case [required: Any, installed: 0.2.0]
    - PyYAML [required: Any, installed: 3.13]
  - Jinja2 [required: Any, installed: 2.10]
    - MarkupSafe [required: >=0.23, installed: 1.0]
  - PyYAML [required: Any, installed: 3.13]
  - requests [required: Any, installed: 2.20.0]
    - certifi [required: >=2017.4.17, installed: 2018.10.15]
    - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
    - idna [required: >=2.5,<2.8, installed: 2.7]
    - urllib3 [required: >=1.21.1,<1.25, installed: 1.24]
  - requests-toolbelt [required: Any, installed: 0.8.0]
    - requests [required: >=2.0.1,<3.0.0, installed: 2.20.0]
      - certifi [required: >=2017.4.17, installed: 2018.10.15]
      - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
      - idna [required: >=2.5,<2.8, installed: 2.7]
      - urllib3 [required: >=1.21.1,<1.25, installed: 1.24]

調試運行方式:

 

# 調試運行 hrun
$ pipenv run python main-debug.py hrun -h

# 調試運行 locusts
$ pipenv run python main-debug.py locusts -h

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章