python 装饰器调用其他类中的方法
场景:
做自动化是,每次都需要调用登录方法,代码重复比较多,在这种场景下,将登录功能抽取为装饰器demo
class Test:
data= get_yaml_date_by_fillter(filename ='warehouse_case.yaml',filter='whpage')
@ddt.data(*data)
@login
def test_OpenWareHouse_Info(self,data):
try:
for key in data:
result = WarehouseInfo.openWareHouseInfo(self)
if result == data[key]['assert']:
logger.info('%s 测试结果:%s' % (key, '通过'))
self.assertEqual(result,data[key]['assert'])
except:
raise
def login(func):
'''
登录功能抽取为装饰器
:param func:
:return:
'''
# 获取正确登录密码账号登录系统
login_data = get_yaml_date_by_fillter(filename='login.yaml', filter='login')[2]
def wrapper(self,*args):
# 调用登录方法
result = LoginModel.login_success(self,login_data['login_case_003'])
if result != None:
return func(self,*args)
else:
print('登录失败')
return wrapper