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"]

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM