httprunner 2.x學習8-參數化(引用 debugtalk 函數)


前言

httprunner 參數化數據源指定支持三種方式:

  • 在 YAML/JSON 中直接指定參數列表:該種方式最為簡單易用,適合參數列表比較小的情況
  • 通過內置的 parameterize(可簡寫為P)函數引用 CSV 文件:該種方式需要准備 CSV 數據文件,適合數據量比較大的情況
  • 調用 debugtalk.py 中自定義的函數生成參數列表:該種方式最為靈活,可通過自定義 Python 函數實現任意場景的數據驅動機制,當需要動態生成參數列表時也需要選擇該種方式

環境:httprunner==2.5.7

本篇講解調用 debugtalk.py 中自定義的函數生成參數列表,生成的參數列表必須為 list of dict 的數據結構。

單個參數

需對 user_id 進行參數化數據驅動,參數取值范圍為 1001~1004,那么就可以在 debugtalk.py 中定義一個函數,返回參數列表。

def get_user_id():
    return [
        {"user_id": 1001},
        {"user_id": 1002},
        {"user_id": 1003},
        {"user_id": 1004}
    ]

然后,在 YAML/JSON 的 parameters 中就可以通過調用自定義函數的形式來指定數據源。

config:
    name: "demo"

testcases:
-       
    name: login-參數化
    testcase: /path/to/testcase1
    parameters:
        user_id: ${get_user_id()}

另外,通過函數的傳參機制,還可以實現更靈活的參數生成功能,在調用函數時指定需要生成的參數個數。

引用自定義函數

對於具有關聯性的多個參數,實現方式也類似。

例如,在 debugtalk.py 中定義函數 get_account,生成指定數量的賬號密碼參數列表。

def get_account(num):
    accounts = []
    for index in range(1, num+1):
        accounts.append(
            {"username": "user%s" % index, "password": "123456"},
        )

    return accounts

那么在 YAML/JSON 的 parameters 中就可以調用自定義函數生成指定數量的參數列表。

config:
    name: "demo"

testcases:
-       
    name: login-參數化
    testcase: /path/to/testcase1
    parameters:
        username-password: ${get_account(10)}

使用案例

debugtalk.py 中定義 get_user_password 函數,返回 10 個用戶名和密碼數據。

# debugtalk.py
def get_account(num):
    user_password = []
    for index in range(1, int(num)+1):
        user_password.append(
            {"user": "test%s" % index, "password": "123456"},
        )

    return user_password

if __name__ == '__main__':
    print(get_account(10))

生成 list of dict 數據格式

[
{'user': 'test1', 'password': '123456'}, 
{'user': 'test2', 'password': '123456'}, 
{'user': 'test3', 'password': '123456'}, 
{'user': 'test4', 'password': '123456'}, 
{'user': 'test5', 'password': '123456'}, 
{'user': 'test6', 'password': '123456'}, 
{'user': 'test7', 'password': '123456'}, 
{'user': 'test8', 'password': '123456'}, 
{'user': 'test9', 'password': '123456'}, 
{'user': 'test10', 'password': '123456'}
]

testsuites/test_params_function.yml 文件內容

# testsuites/test_params_function.yml
# 上海-悠悠,httprunner QQ交流群:717225969
config:
    name: "test login parameters"

testcases:
-       
    name: login-參數化
    testcase: case/test_login.yml
    parameters:
        user-password: ${get_account(5)}

運行結果,生成 5 條用例

(venv_hrun) D:\soft\venu_hrun>hrun testsuites/test_params_function.yml
INFO     HttpRunner version: 2.5.7
INFO     Start to run testcase: login-參參數數化化
login-setup
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 369.83 ms, response_length: 110 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.385s

OK
INFO     Start to run testcase: login-參參數數化化
login-setup
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 347.42 ms, response_length: 110 bytes

.

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

OK
INFO     Start to run testcase: login-參參數數化化
login-setup
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 338.0 ms, response_length: 110 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.338s

OK
INFO     Start to run testcase: login-參參數數化化
login-setup
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 331.99 ms, response_length: 110 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.332s

OK
INFO     Start to run testcase: login-參參數數化化
login-setup
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 354.24 ms, response_length: 110 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.354s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\venu_hrun\reports\20200613T030252.852803.html


httprunner 2.x實戰教程點我 ->立即報名


免責聲明!

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



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