python自動化測試中裝飾器@ddt與@data源碼深入解析
一、使用ddt和data裝飾器的大致框架如下,每個test_開頭的方法,代表一條測試用例
from ddt import ddt,data import unittest test_datas=[ {'id':1,'title':'測試用例1'}, {'id':2,'title':'測試用例2'}, {'id':3,'title':'測試用例3'}, {'id':4,'title':'測試用例4'} ] @ddt class TestDemo(unittest.TestCase): @data(*test_datas) def test_demo1(self,item): print('測試用例執(zhí)行',item)
unittest中的測試用例:
測試類中每一個test開頭的方法就是一條測試用例
ddt根據(jù)用例數(shù)據(jù)生成測試用例的思路:
1、利用data裝飾器:傳入測試數(shù)據(jù),在裝飾器中將測試數(shù)據(jù)保存起來
2、ddt這個裝飾器:遍歷測試數(shù)據(jù),每遍歷出一條數(shù)據(jù),往測試類中添加一個test開頭的方法
setattr(類,方法名,方法)
二、給類動態(tài)的增加方法
案例1
setattr(對象/類,屬性名/方法名,屬性值/方法)
特別注意:
給類動態(tài)增加方法一定要加self
class Demo: def test_1(self): print("這個是方法test_1") def kobe(self,item): print("kobe-----執(zhí)行了",item) datas=[2,8,23,22,24] #根據(jù)數(shù)據(jù)動態(tài)給測試類中增加5個方法 for i in datas: name='test_1_{}'.format(i) #給類動態(tài)增加方法 setattr(Demo,name,kobe) print(Demo.__dict__)
案例2:
調(diào)用動態(tài)執(zhí)行的5個方法,執(zhí)行結(jié)果都為kobe-----執(zhí)行了 24,有bug
class Demo: def test_1(self): print("這個是方法test_1") def kobe(self,item): print("kobe-----執(zhí)行了",item) datas=[2,8,23,22,24] #根據(jù)數(shù)據(jù)動態(tài)給測試類中增加5個方法 for i in datas: name='test_1_{}'.format(i) def wrapper(self): kobe(self,i) #給類動態(tài)增加方法 setattr(Demo,name,wrapper) #print(Demo.__dict__) Demo().test_1_2() Demo().test_1_8() Demo().test_1_22() Demo().test_1_23() Demo().test_1_24()
執(zhí)行結(jié)果:
kobe-----執(zhí)行了 24
kobe-----執(zhí)行了 24
kobe-----執(zhí)行了 24
kobe-----執(zhí)行了 24
kobe-----執(zhí)行了 24
原因分析
案例3:
解決案例2的bug
定義閉包create_method:進(jìn)行數(shù)據(jù)鎖定,鎖定的是datas=[2,8,23,22,24]
class Demo: def test_1(self): print("這個是方法test_1") def kobe(self,item): print("kobe-----執(zhí)行了",item) datas=[2,8,23,22,24] #todo 使用閉包進(jìn)行數(shù)據(jù)鎖定 def create_method(i): def wrapper(self): kobe(self,i) return wrapper #根據(jù)數(shù)據(jù)動態(tài)給測試類中增加5個方法 for i in datas: name='test_1_{}'.format(i) wrapper=create_method(i) #給類動態(tài)增加方法 setattr(Demo,name,wrapper) Demo().test_1_2() Demo().test_1_8() Demo().test_1_22() Demo().test_1_23() Demo().test_1_24()
三、ddt和data的源碼解析
from ddt import ddt,data import unittest test_datas=[ {'id':1,'title':'測試用例1'}, {'id':2,'title':'測試用例2'}, {'id':3,'title':'測試用例3'}, {'id':4,'title':'測試用例4'} ] def ddt(cls): '''遍歷測試數(shù)據(jù),給類動態(tài)添加方法''' #如何通過類獲取方法? #res=cls.__dict__ #print('測試類的方法和屬性字典',res) for name,method in list(cls.__dict__.items()): #遍歷出來的屬性值(方法)是否擁有datas屬性(測試數(shù)據(jù)) if hasattr(method,'datas'): #獲取方法中保存的測試數(shù)據(jù) datas=getattr(method,'datas') #遍歷測試數(shù)據(jù) for index,value in enumerate(datas): print("數(shù)據(jù):",value) #給測試類動態(tài)添加用例 method_name='{}_{}'.format(name,index+1) print('方法名',method_name) #給類動態(tài)的增加方法 def wrapper(self): method(self, value) #todo 給測試類動態(tài)添加一個測試方法 setattr(cls,method_name,wrapper) return cls def data(*args): '''將測試數(shù)據(jù)保存為測試方法的屬性''' #*args接收到的是data裝飾器傳遞進(jìn)來的數(shù)據(jù) def wrapper(func): #func接收的是data裝飾的函數(shù) func.datas=args return func return wrapper @ddt class TestDemo(): @data(*test_datas) #test_demo1=data(*test_datas)(test_demo1) def test_demo1(self,item): print('測試用例執(zhí)行',item) #print(TestDemo.test_demo1.__dict__)
這樣寫的話有bug
原因:
解決:
采用閉包進(jìn)行數(shù)據(jù)鎖定,鎖定value和method
def create_test_method(method,value): def wrapper(self): method(self, value) return wrapper
from ddt import ddt,data import unittest test_datas=[ {'id':1,'title':'測試用例1'}, {'id':2,'title':'測試用例2'}, {'id':3,'title':'測試用例3'}, {'id':4,'title':'測試用例4'} ] def create_test_method(method,value): def wrapper(self): method(self, value) return wrapper def ddt(cls): #todo @ddt這個裝飾器:遍歷測試數(shù)據(jù),每遍歷出一條數(shù)據(jù),往測試類中添加一個test開頭的方法 #setattr(類,方法名,方法) res=list(cls.__dict__.items()) print(res) for name,method in res: print(name,method) if hasattr(method,'datas'): #如果有datas屬性,獲取方法中保存的datas datas=getattr(method,'datas') #遍歷測試數(shù)據(jù) for index,value in enumerate(datas): print('測試數(shù)據(jù):',value) #給測試類動態(tài)的增加測試用例 method_name='{}_{}'.format(name,index+1) print('方法:',method_name,method) #todo 給類動態(tài)的增加方法,最終希望執(zhí)行def test_demo1(self,item):這個方法的 #test_method=method #但是item需要自己傳,但是unittest是不需要傳遞參數(shù)的 # def wrapper(self): # method(self,value) wrapper=create_test_method(method, value) # todo 給測試類動態(tài)添加一個測試方法 setattr(cls, method_name, wrapper) else: delattr(cls,name) return cls def data(*args): # *args為給裝飾器傳遞的參數(shù)test_datas def wrapper(func): # func為被裝飾器裝飾的函數(shù)test_demo1 #todo @data裝飾器的作用是保存測試數(shù)據(jù),將測試數(shù)據(jù)存放到函數(shù)屬性中 func.datas = test_datas return func return wrapper @ddt class TestDemo(unittest.TestCase): @data(*test_datas) #test_demo1=data(*test_datas)(test_demo1) def test_demo1(self,item): print('測試用例執(zhí)行',item)
分部解析代碼
@data(*test_datas) def test_demo1(self,item): print('測試用例執(zhí)行',item)
1、上面3行代碼可以寫成如下:
@data(*test_datas)
:可以表示為test_demo1=data(*test_datas
)(test_demo1)
2、輸出屬性(方法)名稱和屬性值
for name,method in list(cls.__dict__.items())
3、將遍歷出來的屬性名(方法)判斷是否包含datas屬性,如果有datas屬性,獲取方法中保存的datas
if hasattr(method,'datas'): datas=getattr(method,'datas')
總結(jié)
到此這篇關(guān)于python自動化測試中裝飾器@ddt與@data源碼解析的文章就介紹到這了,更多相關(guān)python裝飾器@ddt和@data源碼解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python獲取時光網(wǎng)電影數(shù)據(jù)的實例代碼
這篇文章主要介紹了Python獲取時光網(wǎng)電影數(shù)據(jù),基本原理是先通過requests庫,通過時光網(wǎng)自帶的電影數(shù)據(jù)API接口,獲取到指定的電影數(shù)據(jù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Pandas數(shù)據(jù)分析之pandas文本處理
這篇文章主要介紹了Pandas數(shù)據(jù)分析之pandas文本處理,pandas對文本數(shù)據(jù)也有很多便捷處理方法,可以不用寫循環(huán),向量化操作運算速度快,還可以進(jìn)行高級的正則表達(dá)式,各種復(fù)雜的邏輯篩選和匹配提取信息2022-08-08詳解Django項目中模板標(biāo)簽及模板的繼承與引用(網(wǎng)站中快速布置廣告)
這篇文章主要介紹了詳解Django項目中模板標(biāo)簽及模板的繼承與引用【網(wǎng)站中快速布置廣告】,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03