前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Pytest篇】装饰器@pytest.mark.parametrize多样参数化(二)

【Pytest篇】装饰器@pytest.mark.parametrize多样参数化(二)

作者头像
王大力测试进阶之路
发布2019-11-06 10:24:28
1.5K0
发布2019-11-06 10:24:28
举报
文章被收录于专栏:橙子探索测试橙子探索测试

上一篇:Python Pytest装饰器@pytest.mark.parametrize详解

Pytest中装饰器@pytest.mark.parametrize('参数名',list)可以实现测试用例参数化,类似DDT

1、第一个参数是字符串,多个参数中间用逗号隔开

2、第二个参数是list,多组数据用元祖类型;传三个或更多参数也是这样传。list的每个元素都是一个元组,元组里的每个元素和按参数顺序一一对应

3、传一个参数 @pytest.mark.parametrize('参数名',list) 进行参数化

4、传两个参数@pytest.mark.parametrize('参数名1,参数名2',[(参数1_data[0], 参数2_data[0]),(参数1_data[1], 参数2_data[1])]) 进行参数化,当装饰给方法时,这时方法被被执行2次,第1次:参数名1 对应值 参数1_data[0],参数名2 对应值 参数2_data[0];第2次:参数名1 对应值 参数1_data[1],参数名2 对应值 参数2_data[1],这样就可以用到我们测试用例执行中,根据用例的多少,调用多次,断言多次,不需要用循环去写了。

我们试着尝试list里面嵌套字符串、列表、元祖、字典时是如何处理的,请看下面脚本执行情况

import pytest
 
 
class Test(object):
    #列表
    #====参数为列表====
    @pytest.mark.parametrize('a',[1])
    def test01(self,a):
        print(type(a),a)
    @pytest.mark.parametrize('b',[1,2]) #test02被调用2次
    def test02(self,b):
        print(type(b),b)
 
 
    #列表套元祖
    #====参数为列表嵌套元祖====
    @pytest.mark.parametrize('c',[(3,4)])
    def test03(self,c):
        print(type(c),c)
    @pytest.mark.parametrize('d,e',[(5,6)])
    def test04(self,d,e):
        print(type(d),type(e),d,e)
    @pytest.mark.parametrize('f',[(7,8),(9,10)])#test05被调用2次
    def test05(self,f):
        print(type(f),f)
    @pytest.mark.parametrize('g,h',[(11,12),(13,14)])#test06被调用2次
    def test06(self,g,h):
        print(type(g),type(h),g,h)
 
 
    #列表套字典
    #====参数为列表嵌套字典====
    @pytest.mark.parametrize('i',[{15,16}])
    def test07(self,i):
        print(type(i),i)
    @pytest.mark.parametrize('j,k',[{17,18}])
    def test08(self,j,k):
        print(type(j),type(k),j,k)
    @pytest.mark.parametrize('l',[{19,20},{21,22}])#test14被调用2次
    def test09(self,l):
        print(type(l),l)
    @pytest.mark.parametrize('m,n',[{23,24},{25,26}])#test14被调用2次
    def test10(self,m,n):
        print(type(m),type(n),m,n)
 
 
    # 列表套列表
    #====参数为列表嵌套列表====
    @pytest.mark.parametrize('o',[[27,28]])
    def test011(self,o):
        print(type(o),o)
    @pytest.mark.parametrize('p,q',[[29,30]])
    def test12(self,p,q):
        print(type(p),type(q),p,q)
    @pytest.mark.parametrize('r',[[31,32],[33,34]])#test14被调用2次
    def test13(self,r):
        print(type(r),r)
    @pytest.mark.parametrize('s,t',[[35,36],[37,38]])#test14被调用2次
    def test14(self,s,t):
        print(type(s),type(s),s,t)
if __name__=="__main__":
    pytest.main(["-s","test02.py"])
 
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test02.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 21 items
 
test02.py <class 'int'> 1
.<class 'int'> 1
.<class 'int'> 2
.<class 'tuple'> (3, 4)
.<class 'int'> <class 'int'> 5 6
.<class 'tuple'> (7, 8)
.<class 'tuple'> (9, 10)
.<class 'int'> <class 'int'> 11 12
.<class 'int'> <class 'int'> 13 14
.<class 'set'> {16, 15}
.<class 'int'> <class 'int'> 17 18
.<class 'set'> {19, 20}
.<class 'set'> {21, 22}
.<class 'int'> <class 'int'> 24 23
.<class 'int'> <class 'int'> 25 26
.<class 'list'> [27, 28]
.<class 'int'> <class 'int'> 29 30
.<class 'list'> [31, 32]
.<class 'list'> [33, 34]
.<class 'int'> <class 'int'> 35 36
.<class 'int'> <class 'int'> 37 38
.
 
============================= 21 passed in 0.09s ==============================
 
Process finished with exit code 0

从上面尝试中我们可以看出,装饰器@pytest.mark.parametrize('参数名',list)装饰给方法,list里可以嵌套字符串、列表、字典、元祖,可根据具体情况去使用,当len(list)>1时,方法会被调用多次执行。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档