pytest之参数化

pytest的数据驱动叫做:参数化
unittest的数据驱动叫做:ddt (data driveren test)

用法1:

在测试用例的前面加上:@pytest.mark.parametrize(“参数名”,列表数据)
参数名:用来接收每一项数据,并作为测试用例的参数
列表数据:一组测试数据
参数化示例
多个参数:@pytest.mark.parametrize(“参数1,参数2”,[(数据1,数据2),(数据1,数据2)])

@pytest.mark.parametrize("a,b,c",[(1,3,4),(10,35,45),(22.22,22.22,44.44)])
def test_add(a,b,c):
	res = a + b
	assert res == c
"""
运行结果:
ceshi.py::test_add[1-3-4] PASSED
ceshi.py::test_add[10-35-45] PASSED
ceshi.py::test_add[22.22-22.22-44.44] PASSED
============================== 3 passed in 0.03s ==============================
"""

用法2:

组合参数化:多组参数,依次组合
使用多个@pytest.mark.parametrize
示例:用例有4个:0,2/0,3/1,2/1,3 迪卡尔积 --数据库表连接?

@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
	print(x + y)
"""
运行结果
ceshi.py::test_foo[2-0] 2
PASSED
ceshi.py::test_foo[2-1] 3
PASSED
ceshi.py::test_foo[3-0] 3
PASSED
ceshi.py::test_foo[3-1] 4
PASSED
============================== 4 passed in 0.06s ==============================
"""
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值