httprunner 2.x學習3-variables變量聲明與引用


前言

在 HttpRunner 中,支持變量聲明(variables)和引用($var)的機制。在 config 和 test 中均可以通過 variables 關鍵字定義變量,然后在測試步驟中可以通過 $ + 變量名稱 的方式引用變量。
區別在於

  • 在 config 中定義的變量為全局的,整個測試用例(testcase)的所有地方均可以引用;
  • 在 test 中定義的變量作用域僅局限於當前測試步驟(teststep)
    環境:httprunner==2.5.7

局部變量

在登錄案例中,賬號和密碼是寫死的,一般寫用例的時候,我們最好把這種可能會變的參數單獨寫個變量。做到測試數據和代碼的分離,以便后續維護。
如果我們在test下聲明的變量,作用域只在當前test下有效。聲明變量用variables,變量和對應值用鍵值對,如

- test:
    name: login case1
    variables:
        user: test
        psw: 123456

引用user和psw變量用$user,$psw,完整的test_var.yml腳本如下

# 上海悠悠,httprunner QQ交流群:1121184576
- config:
    name: logincase
    variables: {}
- test:
    name: login case1
    variables:
        user: test
        psw: 123456
    request:
        url: http://127.0.0.1:8000/api/v1/login/
        method: POST
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: $user
            password: $psw
    extract:
        - token: content.token         # 提取token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.msg, login success!]
        - eq: [content.code, 0]

局部變量只在當前的test用例生效,在其它的test用例不會生效

運行用例

使用hrun運行結果

(venv_hrun) D:\soft\venu_hrun>hrun test_var.yml
INFO     HttpRunner version: 2.5.7
INFO     Start to run testcase: logincase
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 340.96 ms, response_length: 109 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.347s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\venu_hrun\reports\20200612T123543.624188.html

全局變量

如果要設置一個全局變量,需把變量聲明(variables)放到config下,這樣就在整個.yml文件生效了

# 上海悠悠,httprunner QQ交流群:717225969
- config:
    name: logincase
    variables:
        user: test
        psw: 123456
- test:
    name: login case1
    request:
        url: http://127.0.0.1:8000/api/v1/login/
        method: POST
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: $user
            password: $psw
    extract:
        - token: content.token         # 提取token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.msg, login success!]
        - eq: [content.code, 0]

運行結果

(venv_hrun) D:\soft\venu_hrun>hrun test_config_var.yml
INFO     HttpRunner version: 2.5.7
INFO     Start to run testcase: logincase
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 337.67 ms, response_length: 109 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.342s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\venu_hrun\reports\20200612T123701.200901.html
Sentry is attempting to send 0 pending error messages


免責聲明!

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



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