• 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:
        testcase1_name:
            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": str(index) * 6},
            )
    
        return accounts
    

    那么在 YAML/JSON 的 parameters 中就可以调用自定义函数生成指定数量的参数列表。

    config:
        name: "demo"
    
    testcases:
        testcase1_name:
            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交流群:1121184576
    config:
        name: "test login parameters"
    
    testcases:
       login-参数化:
            testcase: case/test_login.yml
            parameters:
                user-password: ${get_account(5)}
    

    运行结果,生成 5 条用例

    (venv_hrun) D:softvenu_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:softvenu_hrun
    eports20200613T030252.852803.html
    
  • 相关阅读:
    bzoj5178 [Jsoi2011]棒棒糖 主席树+线段树二分
    bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心
    bzoj3123 [Sdoi2013]森林 树上主席树+启发式合并
    bzoj4448 [Scoi2015]情报传递 主席树+树上差分
    bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集
    CF1009F Dominant Indices 长链剖分
    bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP
    bzoj4009 [HNOI2015]接水果 整体二分+扫描线+树状数组+dfs序
    bzoj4940 [Ynoi2016]这是我自己的发明 莫队+dfs序
    bzoj5016 & loj2254 [Snoi2017]一个简单的询问 莫队
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/13112104.html
Copyright © 2020-2023  润新知