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時,方法會被調用多次執行。