pytest之参数化parametrize的使用

送分小仙女□ 提交于 2019-11-30 00:55:18

在测试用例的前面加上:
@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"]

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!