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

mac2026-08-04  1

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时,方法会被调用多次执行。

最新回复(0)