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 pytestclass 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 itemstest02.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時,方法會被調用多次執行。
總結
以上是生活随笔為你收集整理的Python Pytest装饰器@pytest.mark.parametrize多样参数化(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分享几个接口自动化的实战练手项目
- 下一篇: Appium+Python安卓自动化测试