Pytest参数化测试上期我们介绍到Python自动化测试 | Pytest之参数化 常用的用法如下
@pytest.mark.parametrize("参数名",列表数据)
我们今天在简单的一起学习一下,参数化支持的三种类型
参数化方式支持:列表、元组、字典 三种类型;
我们拿实例代码入参, 一起看一看
# coding=utf-8
# authou:shichao
import pytest
data_lists = [
[11, 22, 33]
]
test_datas = [
(11, 22, 33),
(22, 33, 55)
]
datas_dict = [
{"a": 1, "b": 2, "c": 3},
{"a": 11, "b": 22, "c": 33},
{"a": 111, "b": 222, "c": 333},
]
# 方式一:参数为列表中嵌套列表
@pytest.mark.parametrize("a, b, c", data_lists)
def test_add01(a, b, c):
res = a + b
assert res == c
# 方式二:参数为列表中嵌套元组
@pytest.mark.parametrize("data", test_datas)
def test_add02(data):
res = data[0] + data[1]
assert res == data[2]
# 方式三:参数为列表中嵌套字典
@pytest.mark.parametrize("data", datas_dict)
def test_add03(data):
res = data["a"] + data["b"]
assert res == data["c"]
每天一个小知识点,再忙不忘记充电