httprunner参数化

1. 示例

引入对应的Parameters

1.1. CSV参数

复制代码
from httprunner import HttpRunner, Config, Step, RunRequest, Parameters
@pytest.mark.parametrize(
        "param", Parameters({"mobile_phone-pwd": "${P(csv_data/mobile_phone-pwd.csv)}"})
    )
    def test_start(self, param):
        super().test_start(param)

1.2. 列表参数

复制代码
@pytest.mark.parametrize(
        "param", Parameters({"phone": ["09551234911", "08186897716","07011698742"],
                             "biller": ["GLO", "9mobile","Airtel"]}
                            )
    )
    def test_start(self, param):
        super().test_start(param)

输出效果:笛卡尔积

1.3. 关联参数

复制代码
@pytest.mark.parametrize(
        "param",
        Parameters(
            {
                "user-password": [
                    ["test1", "123456"],
                    ["test2", "123456"],
                    ["test3", "123456"],
                    ["test4", "123456"],
                ]
            }
        ),
    )
    def test_start(self, param):
        super().test_start(param)

@pytest.mark.parametrize(
        "param", Parameters(
            {"mobileNo-biller": [
                ["09551234911", "GLO"],
                ["08186897716", "9MOBILE"],
                ["07011698742", "AIRTEL"]
            ]}
        )
    )
    def test_start(self, param):
        super().test_start(param)

2. 参数说明

参数名称的定义分为两种情况:

  • 独立参数单独进行定义;
  • 多个参数具有关联性的参数需要将其定义在一起,采用短横线(-)进行连接。

数据源指定支持三种方式:

  • 在 YAML/JSON/py 中直接指定参数列表:该种方式最为简单易用,适合参数列表比较小的情况
  • 通过内置的 parameterize(可简写为P)函数引用 CSV 文件:该种方式需要准备 CSV 数据文件,适合数据量比较大的情况
  • 调用 debugtalk.py 中自定义的函数生成参数列表:该种方式最为灵活,可通过自定义 Python 函数实现任意场景的数据驱动机制,当需要动态生成参数列表时也需要选择该种方式

三种方式可根据实际项目需求进行灵活选择,同时支持多种方式的组合使用。

假如测试用例中定义了多个参数,那么测试用例在运行时会对参数进行笛卡尔积组合,覆盖所有参数组合情况。

相关推荐
张太行_6 小时前
Linux静态库:多模块高效管理
linux·运维·服务器
ID_180079054736 小时前
YouTube item_get_video接口认证方式:API密钥与OAuth2.0的选择与应用
服务器·windows·microsoft
wgl6665207 小时前
Linux---基础IO!
linux·运维·服务器
Ancelin安心7 小时前
kali-dirsearch的使用
linux·运维·服务器·python·计算机网络·web安全·网络安全
jun_bai8 小时前
python+Java的网盘程序升级版。无感知备份文档,保护数据资产利器。
运维·服务器
上海云盾-高防顾问8 小时前
筑牢网络防线:境外恶意网址与IP防范指南
服务器·网络·安全
suzhou_speeder8 小时前
PoE 延长器:突破 PoE 距离限制,优化网络灵活部署方案
运维·网络·poe·poe交换机·poe延长器
月光下的麦克8 小时前
如何查案动态库版本
linux·运维·c++
EndingCoder9 小时前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35289 小时前
Web集群管理实战指南:从架构到运维
运维·前端·架构