pytest之参数化parametrize的使用

在测试用例的前面加上:
@pytest.mark.parametrize("参数名",列表数据)
参数名:用来接收每一项数据,并作为测试用例的参数。
列表数据:一组测试数据。

import pytest
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", [(1, 2, 3), (4, 5, 9)])
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"]

  

原文地址:https://www.cnblogs.com/lexus168/p/13776279.html