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 函数实现任意场景的数据驱动机制,当需要动态生成参数列表时也需要选择该种方式

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

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

相关推荐
柳鲲鹏几秒前
交叉编译:strip: Unable to recognise the format of the input file xx.c.o
linux·运维·服务器
Le1Yu28 分钟前
微服务拆分以及注册中心
linux·运维·服务器
zzzsde1 小时前
【Linux】linux基础指令入门(1)
linux·运维·学习
moxiaoran57531 小时前
nginx中proxy_pass配置
运维·服务器·nginx
weixin_379880921 小时前
.Net Core WebApi集成Swagger
java·服务器·.netcore
leellun1 小时前
在Centos上安装Python指定版本
linux·运维·centos
她说彩礼65万1 小时前
Asp.net core Kestrel服务器详解
服务器·后端·asp.net
ZLRRLZ2 小时前
【Linux操作系统】进程控制
linux·运维·服务器
我想吃余2 小时前
Linux的Ext文件系统:硬盘理解和inode及软硬链接
linux·运维·电脑