httprunner 3.x學習1 - 環境安裝與准備


前言

httprunner 3.x最大的改變是執行用例用的是 python 的 pytest 框架,支持3種格式的用例:YAML/JSON/pytest 代碼
對比 httprunner 2.x 以前版本,早期版本用的是 unittest 框架,只支持2種格式的用例: YAML/JSON

簡介

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

  • 繼承的所有強大功能requests ,只需以人工方式獲得樂趣即可處理HTTP(S)。
  • 以YAML或JSON格式定義測試用例,pytest 以簡潔優雅的方式運行。
  • 在HAR 支持下記錄並生成測試用例。
  • 支持variables/ extract/ validate/hooks機制,以創建非常復雜的測試方案。
  • 使用debugtalk.py插件,任何功能都可以在測試用例的任何部分使用。
  • 使用jmespath ,提取和驗證json響應從未如此簡單。
  • 有了pytest ,數百個插件隨時可用。
  • 使用allure ,測試報告可以非常強大。
  • 通過重復使用locust ,您可以進行性能測試,而無需進行額外的工作。
  • 支持CLI命令,與CI/CD完美結合。

環境安裝

使用pip 安裝

pip3 install httprunner

如果之前裝過低版本的,可以加 -U 參數升級到最新版

pip3 install httprunner -U

也可以支持 github 源碼安裝

pip3 install git+https://github.com/httprunner/httprunner.git@master

查看版本號 v3.1.4

C:\Users\dell>pip show httprunner
Name: httprunner
Version: 3.1.4
Summary: One-stop solution for HTTP(S) testing.
Home-page: https://github.com/httprunner/httprunner
Author: debugtalk
Author-email: debugtalk@gmail.com
License: Apache-2.0
Location: e:\python36\lib\site-packages
Requires: loguru, pytest-html, pydantic, pytest, requests, black, jinja2, sentry-sdk, jmespath, pyyaml
Required-by:

相關命令

安裝 HttpRunner 后,以下 5 個命令會寫入系統環境變量配置。

  • httprunner:主命令,用於所有功能。
  • hrun:指令 httprunner run 的別名,用於運行 YAML/JSON/Pytest 測試用例。
  • hmake: 指令 httprunner make 的別名,將 YAML/JSON 用例轉換成 pytest 用例。
  • har2case:指令 httprunner har2case 的別名,將 HAR 文件轉換成 YAML/JSON 用例。
  • locust: 利用 locust 運行性能測試。

httprunner -h 查看相關命令

C:\Users\dell>httprunner -h
usage: httprunner [-h] [-V] {run,startproject,har2case,make} ...

One-stop solution for HTTP(S) testing.

positional arguments:
  {run,startproject,har2case,make}
                        sub-command help
    run                 Make HttpRunner testcases and run with pytest.
    startproject        Create a new project with template structure.
    har2case            Convert HAR(HTTP Archive) to YAML/JSON testcases for
                        HttpRunner.
    make                Convert YAML/JSON testcases to pytest cases.

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show version

主要有四個參數 run,startproject,har2case,make

參數名 功能
-V --version 查看版本號
-h, --help 查看幫助
run 用於運行YAML/JSON/Pytest 測試用例
startproject 使用模板結構創建新項目
har2case HAR(HTTP Archive) 轉成 YAML/JSON
make YAML/JSON 測試用例轉成 pytest用例

最大的變化是新增了make參數,把 YAML/JSON 測試用例轉成 pytest用例

startproject 創建項目

startproject 參數可以快速創建一個 demo 項目,方便了解整個項目結構

D:\demo\hrun3x>httprunner startproject yoyo_demo
2021-06-10 22:29:30.482 | INFO     | httprunner.scaffold:create_scaffold:43 - Create new project: yoyo_demo
Project Root Dir: D:\demo\hrun3x\yoyo_demo

created folder: yoyo_demo
created folder: yoyo_demo\har
created folder: yoyo_demo\testcases
created folder: yoyo_demo\reports
created file: yoyo_demo\testcases\demo_testcase_request.yml
created file: yoyo_demo\testcases\demo_testcase_ref.yml
created file: yoyo_demo\debugtalk.py
created file: yoyo_demo\.env
created file: yoyo_demo\.gitignore

$ tree yoyo_demo -a
2021-06-10 22:29:30.504 | WARNING  | httprunner.scaffold:show_tree:29 - tree command not exists, ignore.
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-Break to quit

需注意的是這里用的是httprunner 命令(並不是hrun, 運行完成后可以看到整個項目結構如下

make 轉 pytest 用例

make 是把 YAML/JSON 測試用例轉成 pytest用例, 需指定 yaml 文件路徑或文件夾路徑

D:\demo\hrun3x\yoyo_demo>httprunner make testcases
2021-06-10 22:40:08.048 | INFO     | httprunner.make:__make:512 - make path: D:\demo\hrun3x\yoyo_demo\testcases
2021-06-10 22:40:08.053 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.057 | INFO     | httprunner.loader:load_dot_env_file:127 - Loading environment variables from D:\demo\hrun3x\yoyo_demo\.env
2021-06-10 22:40:08.060 | DEBUG    | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME
2021-06-10 22:40:08.062 | DEBUG    | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD
2021-06-10 22:40:08.064 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref.yml
2021-06-10 22:40:08.075 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.077 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request.yml
2021-06-10 22:40:08.080 | INFO     | httprunner.make:make_testcase:442 - generated testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request_test.py
2021-06-10 22:40:08.082 | INFO     | httprunner.make:make_testcase:442 - generated testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref_test.py
2021-06-10 22:40:08.094 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.097 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request.yml
2021-06-10 22:40:08.098 | INFO     | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
reformatted D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref_test.py
reformatted D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request_test.py
All done! ✨✨ ���� ✨✨
2 files reformatted.

執行后會生成對應的 pytest 用例

testcases\demo_testcase_request_test.py 完整代碼示例如下

# NOTE: Generated By HttpRunner v3.1.4
# FROM: testcases\demo_testcase_request.yml


from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseDemoTestcaseRequest(HttpRunner):

    config = (
        Config("request methods testcase with functions")
        .variables(
            **{
                "foo1": "config_bar1",
                "foo2": "config_bar2",
                "expect_foo1": "config_bar1",
                "expect_foo2": "config_bar2",
            }
        )
        .base_url("https://postman-echo.com")
        .verify(False)
        .export(*["foo3"])
    )

    teststeps = [
        Step(
            RunRequest("get with params")
            .with_variables(
                **{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}"}
            )
            .get("/get")
            .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
            .with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
            .extract()
            .with_jmespath("body.args.foo2", "foo3")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args.foo1", "bar11")
            .assert_equal("body.args.sum_v", "3")
            .assert_equal("body.args.foo2", "bar21")
        ),
        Step(
            RunRequest("post raw text")
            .with_variables(**{"foo1": "bar12", "foo3": "bar32"})
            .post("/post")
            .with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "text/plain",
                }
            )
            .with_data(
                "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
            )
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal(
                "body.data",
                "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32.",
            )
        ),
        Step(
            RunRequest("post form data")
            .with_variables(**{"foo2": "bar23"})
            .post("/post")
            .with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }
            )
            .with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.form.foo1", "$expect_foo1")
            .assert_equal("body.form.foo2", "bar23")
            .assert_equal("body.form.foo3", "bar21")
        ),
    ]


if __name__ == "__main__":
    TestCaseDemoTestcaseRequest().test_start()

run 執行用例

run 命令實際上有 2 個動作,一個是把 YAML/JSON 測試用例轉成 pytest 用例,同上一步 make 的功能一樣。
第二個動作是用 pytest 執行測試用例,使用命令如下

httprunner run testcases

運行結果可以看出是用的 pytest 運行的測試用例

collected 2 items                                                                                                                                                                                                           

testcases\demo_testcase_request_test.py .                                                                                                                                                                             [ 50%]
testcases\demo_testcase_ref_test.py .   

執行的過程中會先把 yaml 文件轉成對應的 py 文件,在 testcases 目錄可以看到

har2case

har2case 是把抓包抓到的 .har 文件轉成 YAML/JSON 格式用例,參考前面的博客https://www.cnblogs.com/yoyoketang/p/11564028.html
跟之前老版本功能一樣,不再重復講

hrun

hrunhttprunner run 的別名,所以

httprunner run testcases

等價於

hrun testcases

hmake

hmake 是 httprunner make 的別名,所以

httprunner make testcases

等價於

hmake testcases

locust

利用locust 運行性能測試, 參考前面locust相關教程https://www.cnblogs.com/yoyoketang/p/11644998.html

知識儲備

如果想更好的掌握 httprunner 3.x 框架,最好有以下知識儲備

  • python3 基礎語法,畢竟httprunner 3.x 主要新增pytest用例,鼓勵大家寫代碼了
  • requests 庫,發 HTTP(HTTPS) 請求基本庫
  • YAML/JSON 文件語法
  • jmespath 提取返回結果,類似於 jsonpath 庫,可以先了解下
  • pytest 框架,執行用例最終用的是 pytest 框架,熟練掌握 pytest 框架是很有必要的
  • allure 測試報告用的 allure 框架
  • httprunner 2.x 語法,之前學的老版本 1.x 和 2.x 的語法並不過時,有這個基礎學起來更容易掌握
  • locust 如果想做性能測試,可以掌握這個框架

網易雲課程地址



點我 ->立即報名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM