前言httprunner 3.x 支持3種格式的用例:YAML/JSON/pytest 代碼,3.x版本主推的是pytest測試用例。 測試用例結(jié)構(gòu)httprunner 3.x 版本弱化了api層的概念,直接在 testcase 中寫request 請求,如果是單個請求,也可以直接寫成一個 testcase 。 每個 testcase 必須具有兩個類屬性:config 和 teststeps。 每個測試用例都應(yīng)該有一個config部分,您可以在其中配置測試用例級別的設(shè)置,有以下屬性 屬性名稱 | 是否必填 | 作用 |
---|
name | 必填 | 指定測試用例名稱。這將顯示在執(zhí)行日志和測試報(bào)告中。 | base_url | 可選 | 如果base_url指定,則 teststep 中的 url 可以設(shè)置相對路徑部分 | verify | 可選 | https請求時,是否校驗(yàn)證書,默認(rèn)True,忽略證書校驗(yàn)可以設(shè)置為False | variables | 可選 | 指定測試用例的公共變量。每個測試步驟都可以引用未在步驟變量中設(shè)置的配置變量。換句話說,步驟變量比配置變量具有更高的優(yōu)先級。 | export | 可選 | (早期版本用的output)指定導(dǎo)出的測試用例會話變量,把變量暴露出來,設(shè)置為全局變量 |
teststeps 可以有多個步驟,每個步驟對應(yīng)一個接口的請求,也就是 RunRequest (測試步驟) 屬性名稱 | 是否必填 | 作用 |
---|
name | 必填 | 指定測試步驟名稱 | method(url) | 必填 | 。如果在Config中設(shè)置了baseurl,method中只能設(shè)置相對路徑,可選參數(shù)為get/post/put/delete/等 | with_params | 可選 | 對應(yīng)于的params參數(shù)requests.request | with_headers | 可選 | 對應(yīng)于的headers參數(shù)requests.request | with_cookies | 可選 | cookies參數(shù)requests.request | with_data | 可選 | cookies參數(shù)requests.request | with_cookies | 可選 | cookies參數(shù)requests.request | with_data | 可選 | 對應(yīng)于的data參數(shù)requests.request | with_json | 可選 | 對應(yīng)于的json參數(shù)requests.request | with_variables | 可選 | 指定測試步驟變量。每個步驟的變量都是獨(dú)立的,參數(shù)引用使用"$變量名",如果是函數(shù)引用使用"${函數(shù)名()}" |
extract 數(shù)據(jù)提取with_jmespath(jmes_path:文字,var_name:文字) validate 校驗(yàn)結(jié)果使用jmespath提取 JSON 響應(yīng)正文并使用預(yù)期值進(jìn)行驗(yàn)證。 assert_XXX(jmes_path: Text, expected_value: Any, message: Text = "") 下圖顯示了 HttpRunner 內(nèi)置驗(yàn)證器。
yaml 結(jié)構(gòu) testcaseyaml 結(jié)構(gòu) testcase 和之前2.x版本沒什么區(qū)別,以登錄接口為例test_login.yml # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/
config: name: login case base_url: http://127.0.0.1:8000 export: - token
teststeps: - name: step login variables: user: test1 psw: "123456" request: url: /api/v1/login method: POST json: username: $user password: $psw extract: token: content.token validate: - eq: [status_code, 200] - eq: [content.code, 0] - eq: [content.msg, login success!] - len_eq: [content.token, 40] hrun 運(yùn)行結(jié)果
pytest 腳本hrun 運(yùn)行后在當(dāng)前目錄會自動生成對應(yīng)的 pytest 腳本test_login_test.py # NOTE: Generated By HttpRunner v3.1.4 # FROM: test_login.yml # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseTestLogin(HttpRunner):
config = Config("logincase").base_url("http://127.0.0.1:8000").export(*["token"])
teststeps = [ Step( RunRequest("steplogin") .with_variables(**{"user": "test1", "psw": "123456"}) .post("/api/v1/login") .with_json({"username": "$user", "password": "$psw"}) .extract() .with_jmespath("body.token", "token") .validate() .assert_equal("status_code", 200) .assert_equal("body.code", 0) .assert_equal("body.msg", "login success!") .assert_length_equal("body.token", 40) ), ]
if __name__ == "__main__": TestCaseTestLogin().test_start() 命令行使用pytest -s 運(yùn)行結(jié)果 D:\demo\hrun3x\testcases>pytest test_login_test.py -s ===================== test session starts ================================ platform win32 -- Python 3.6.6, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: D:\demo\hrun3x\testcases plugins: allure-pytest-2.8.6, Faker-8.6.0, assume-2.3.3, base-url-1.4.2 collected 1 item
test_login_test.py 2021-06-15 20:43:49.328 | INFO | httprunner.runner:test_start:451 - Start to run testcase: logincase, TestCase ID: ec9695e0-6e05-4bbf-8276-216b4b09a380 2021-06-15 20:43:49.330 | INFO | httprunner.runner:__run_step:292 - run step begin: steplogin >>>>>> 2021-06-15 20:43:49.476 | DEBUG | httprunner.client:request:186 - client IP: 192.168.1.125, Port: 20657 2021-06-15 20:43:49.477 | DEBUG | httprunner.client:request:194 - server IP: 49.235.92.12, Port: 8201 2021-06-15 20:43:49.478 | DEBUG | httprunner.client:log_print:40 - ================== request details ================== method : POST url : http://127.0.0.1:8000/api/v1/login headers : { "User-Agent": "python-requests/2.22.0", "Accept-Encoding": "gzip, deflate", "Accept": "*/*", "Connection": "keep-alive", "HRUN-Request-ID": "HRUN-ec9695e0-6e05-4bbf-8276-216b4b09a380-029331", "Content-Length": "43", "Content-Type": "application/json" } cookies : {} body : { "username": "test1", "password": "123456" }
2021-06-15 20:43:49.480 | DEBUG | httprunner.client:log_print:40 - ================== response details ================== status_code : 200 headers : { "Date": "Tue, 15 Jun 2021 12:43:49 GMT", "Server": "WSGIServer/0.2 CPython/3.6.8", "Content-Type": "application/json", "Vary": "Accept, Cookie", "Allow": "POST, OPTIONS", "X-Frame-Options": "SAMEORIGIN", "Content-Length": "110" } cookies : {} encoding : None content_type : application/json body : { "code": 0, "msg": "login success!", "username": "test1", "token": "99c06543e23d90f26b3d02e2474120fa6dbb7cc3" }
2021-06-15 20:43:49.483 | INFO | httprunner.client:request:218 - status_code: 200, response_time(ms): 144.13 ms, response_length: 0 bytes 2021-06-15 20:43:49.484 | INFO | httprunner.response:extract:176 - extract mapping: {'token': '99c06543e23d90f26b3d02e2474120fa6dbb7cc3'} 2021-06-15 20:43:49.485 | INFO | httprunner.response:validate:246 - assert status_code equal 200(int) ==> pass 2021-06-15 20:43:49.486 | INFO | httprunner.response:validate:246 - assert body.code equal 0(int) ==> pass 2021-06-15 20:43:49.487 | INFO | httprunner.response:validate:246 - assert body.msg equal login success!(str) ==> pass 2021-06-15 20:43:49.488 | INFO | httprunner.response:validate:246 - assert body.token length_equal 40(int) ==> pass 2021-06-15 20:43:49.489 | INFO | httprunner.runner:__run_step:304 - run step end: steplogin <<<<<<
2021-06-15 20:43:49.489 | INFO | httprunner.runner:test_start:460 - generate testcase log: D:\demo\hrun3x\testcases\logs\ec9695e0-6e05-4bbf-8276-216b4b09a380.run.log .
============================== 1 passed in 0.96s =================================== jmespath相關(guān)語法可以查看這篇https://www.cnblogs.com/yoyoketang/p/14310898.html
|