環境:Ubuntu
- 安裝
sudo pip3 install httprunner==2.5.7
在 HttpRunner 安裝成功後,系統中會新增如下 5 個命令:
httprunner: 核心命令
ate: 曾經用過的命令(當時框架名稱爲 ApiTestEngine),功能與 httprunner 完全相同
hrun: httprunner 的縮寫,功能與 httprunner 完全相同
locusts: 基於 Locust 實現性能測試
har2case: 輔助工具,可將標準通用的 HAR 格式(HTTP Archive)轉換爲YAML/JSON格式的測試用例
- 實例操作
TesterHome 登錄
功能描述:
- 進入登錄頁面
- 輸入賬號和密碼
- 點擊【Sign In】進行登錄
Charles 抓包生成 HAR 文件
點擊文件, 導出session,保存爲har格式。
轉化爲yaml文件
har2case docs/data/testerhome-login.har -2y
執行, 生成html格式的report,在reports目錄下
$ hrun testerhome-sign_in.yml
INFO HttpRunner version: 2.5.7
INFO Start to run testcase: testcase description
/account/sign_in
INFO GET https://testerhome.com/account/sign_in
INFO status_code: 200, response_time(ms): 179.4 ms, response_length: 12115 bytes
.
/account/sign_in
INFO POST https://testerhome.com/account/sign_in
INFO status_code: 200, response_time(ms): 231.92 ms, response_length: 89 bytes
.
/
INFO GET https://testerhome.com/
INFO status_code: 200, response_time(ms): 157.67 ms, response_length: 51701 bytes
.
----------------------------------------------------------------------
Ran 3 tests in 0.577s
OK
INFO Start to render Html report ...
INFO Generated Html report: ./reports/20200610T031226.611388.html
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
$
性能測試:
HttpRunner 通過複用 Locust
,可以在無需對 YAML/JSON 進行任何修改的情況下,直接運行性能測試。
$ locusts -f example-api-test.yml --processes 4
HttpRunner version: 2.5.7
[2020-06-10 11:18:20,853] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting web monitor at http://*:8089
[2020-06-10 11:18:20,854] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting Locust 0.14.6
[2020-06-10 11:18:20,877] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting Locust 0.14.6
[2020-06-10 11:18:20,877] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting Locust 0.14.6
[2020-06-10 11:18:20,877] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting Locust 0.14.6
[2020-06-10 11:18:20,878] wuzhiguo-Lenovo-B490/INFO/locust.main: Starting Locust 0.14.6