pytest参数化的多种使用技巧_ddt使用嵌套列表,讲的明明白白

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
img

正文

说明

当测试用例只需要一个参数时,我们存放数据的列表无序嵌套序列,@pytest.mark.parametrize(‘a’, data)装饰器的第一个参数也只需要一个变量接收列表中的每个元素,第二个参数传递存储数据的列表,那么测试用例需要使用同名的字符串接收测试数据(实例中的a)且列表有多少个元素就会生成并执行多少个测试用例

一组数据

“”"

@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760

“”"
import pytest

data = [
[1, 2, 3],
[4, 5, 9]
] # 列表嵌套列表

data_tuple = [

(1, 2, 3),

(4, 5, 9)

] # 列表嵌套元组

@pytest.mark.parametrize(‘a, b, expect’, data)
def test_parametrize_1(a, b, expect): # 一个参数接收一个数据
print(‘\n测试数据为\n{},{},{}’.format(a, b, expect))
actual = a + b
assert actual == expect

@pytest.mark.parametrize(‘value’, data)
def test_parametrize_2(value): # 一个参数接收一组数据
print(‘\n测试数据为\n{}’.format(value))
actual = value[0] + value[1]
assert actual == value[2]

if name == ‘main’:
pytest.main([‘-s’])

输出

============================= test session starts =============================
platform win32 – Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: E:\CnblogCode\pytest_parametrize, inifile:
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0
collected 4 items

test_parametrize.py
测试数据为
1,2,3
.
测试数据为
4,5,9
.
测试数据为
[1, 2, 3]
.
测试数据为
[4, 5, 9]
.

========================== 4 passed in 0.17 seconds ===========================

Process finished with exit code 0

说明

当测试用例需要多个数据时,我们可以使用嵌套序列(嵌套元组&嵌套列表)的列表来存放测试数据

装饰器@pytest.mark.parametrize()可以使用单个变量接收数据,也可以使用多个变量接收,同样,测试用例函数也需要与其保持一致

当使用单个变量接收时,测试数据传递到测试函数内部时为列表中的每一个元素或者小列表,需要使用索引的方式取得每个数据

当使用多个变量接收数据时,那么每个变量分别接收小列表或元组中的每个元素

列表嵌套多少个多组小列表或元组,测生成多少条测试用例

图解对应关系

组合数据

“”"

@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760

“”"
import pytest

data_1 = [1, 2]
data_2 = [3, 4]

@pytest.mark.parametrize(‘a’, data_1)
@pytest.mark.parametrize(‘b’, data_2)
def test_parametrize_1(a, b):
print(‘\n测试数据为\n{},{}’.format(a, b))

if name == ‘main’:
pytest.main([‘-s’])

输出

============================= test session starts =============================
platform win32 – Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: E:\CnblogCode\pytest_parametrize, inifile:
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0
collected 4 items

test_parametrize.py
测试数据为
1,3
.
测试数据为
2,3
.
测试数据为
1,4
.
测试数据为
2,4
.

========================== 4 passed in 0.24 seconds ===========================

Process finished with exit code 0

说明

通过测试结果,我们不难分析,一个测试函数还可以同时被多个参数化装饰器装饰,那么多个装饰器中的数据会进行交叉组合的方式传递给测试函数,进而生成n*n个测试用例,这也为我们的测试设计时提供了方便

标记用例

可以直接标记测试用例,参数化装饰器也可以识别(标记用例失败或跳过)

标记为无条件跳过(标记为失败为xfail,自己尝试)

“”"

@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760

“”"
import pytest

data_1 = [
[1, 2, 3],
pytest.param(3, 4, 8, marks=pytest.mark.skip)
]

def add(a, b):
return a + b

@pytest.mark.parametrize(‘a, b, expect’, data_1)
def test_parametrize_1(a, b, expect):
print(‘\n测试数据为\n{},{}’.format(a, b))
assert add(a, b) == expect

if name == ‘main’:
pytest.main([‘-vs’])

输出

============================= test session starts =============================
platform win32 – Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0 – C:\Programs\Python\Python37-32\python.exe
cachedir: .pytest_cache
metadata: {‘Python’: ‘3.7.2’, ‘Platform’: ‘Windows-7-6.1.7601-SP1’, ‘Packages’: {‘pytest’: ‘4.3.1’, ‘py’: ‘1.8.0’, ‘pluggy’: ‘0.9.0’}, ‘Plugins’: {‘rerunfailures’: ‘7.0’, ‘metadata’: ‘1.8.0’, ‘html’: ‘1.20.0’}, ‘JAVA_HOME’: ‘D:\JDK’}
rootdir: E:\CnblogCode\pytest_parametrize, inifile:
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0
collecting … collected 2 items

test_parametrize.py::test_parametrize_1[1-2-3]
测试数据为
1,2
PASSED
test_parametrize.py::test_parametrize_1[3-4-8] SKIPPED

===================== 1 passed, 1 skipped in 0.17 seconds =====================

Process finished with exit code 0

说明

输出结果显示收集到2个用例,一个通过,一个被跳过,当我们不想执行某组测试数据时,我们可以标记skip或skipif;当我们预期某组数据会执行失败时,我们可以标记为xfail等

嵌套字典

“”"

@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760

“”"
import pytest

data_1 = (
{
‘user’: 1,
‘pwd’: 2
},
{
‘user’: 3,
‘pwd’: 4
}
)

@pytest.mark.parametrize(‘dic’, data_1)
def test_parametrize_1(dic):
print(‘\n测试数据为\n{}’.format(dic))

if name == ‘main’:
pytest.main([‘-s’])

输出

============================= test session starts =============================
platform win32 – Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: E:\CnblogCode\pytest_parametrize, inifile:
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0
collected 2 items

test_parametrize.py
测试数据为
{‘user’: 1, ‘pwd’: 2}
.
测试数据为
{‘user’: 3, ‘pwd’: 4}
.

========================== 2 passed in 0.20 seconds ===========================

Process finished with exit code 0

增加可读性

使用ids参数

参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示,为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明

在使用前你需要知道,ids参数应该是一个字符串列表,必须和数据对象列表的长度保持一致,我们可以试着使用ids,看下效果

“”"

@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760

“”"
import pytest

data_1 = [
(1, 2, 3),
(4, 5, 9)
]

ids = [“a:{} + b:{} = expect:{}”.format(a, b, expect) for a, b, expect in data_1]

def add(a, b):
return a + b

@pytest.mark.parametrize(‘a, b, expect’, data_1, ids=ids)
class TestParametrize(object):

def test_parametrize_1(self, a, b, expect):
print(‘\n测试函数1测试数据为\n{}-{}’.format(a, b))
assert add(a, b) == expect

def test_parametrize_2(self, a, b, expect):
print(‘\n测试函数2数据为\n{}-{}’.format(a, b))
assert add(a, b) == expect

if name == ‘main’:
pytest.main([‘-v’]) # -v : 更加详细的输出测试结果

输出

装饰器不传递ids参数的输出

collecting … collected 4 items

test_parametrize.py::TestParametrize::test_parametrize_1[1-2-3] PASSED [ 25%]
test_parametrize.py::TestParametrize::test_parametrize_1[4-5-9] PASSED [ 50%]
test_parametrize.py::TestParametrize::test_parametrize_2[1-2-3] PASSED [ 75%]
test_parametrize.py::TestParametrize::test_parametrize_2[4-5-9] PASSED [100%]

========================== 4 passed in 0.16 seconds ===========================

Process finished with exit code 0
装饰器传递ids参数的输出

collecting … collected 4 items

test_parametrize.py::TestParametrize::test_parametrize_1[a:1 + b:2 = expect:3] PASSED [ 25%]
test_parametrize.py::TestParametrize::test_parametrize_1[a:4 + b:5 = expect:9] PASSED [ 50%]
test_parametrize.py::TestParametrize::test_parametrize_2[a:1 + b:2 = expect:3] PASSED [ 75%]
test_parametrize.py::TestParametrize::test_parametrize_2[a:4 + b:5 = expect:9] PASSED [100%]

========================== 4 passed in 0.20 seconds ===========================

Process finished with exit code 0

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
etrize_2[a:4 + b:5 = expect:9] PASSED [100%]

========================== 4 passed in 0.20 seconds ===========================

Process finished with exit code 0

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-2YVloFS2-1713151931554)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值