我在装饰器转移变量 insurance_mode
时遇到问题。我会通过以下装饰器声明来做到这一点:
@execute_complete_reservation(True)
def test_booking_gta_object(self):
self.test_select_gta_object()
但不幸的是,这种说法不起作用。也许也许有更好的方法来解决这个问题。
def execute_complete_reservation(test_case,insurance_mode):
def inner_function(self,*args,**kwargs):
self.test_create_qsf_query()
test_case(self,*args,**kwargs)
self.test_select_room_option()
if insurance_mode:
self.test_accept_insurance_crosseling()
else:
self.test_decline_insurance_crosseling()
self.test_configure_pax_details()
self.test_configure_payer_details
return inner_function
原文由 falek.marcin 发布,翻译遵循 CC BY-SA 4.0 许可协议
带参数的装饰器的语法有点不同——带参数的装饰器应该返回一个函数,该函数将 接受一个函数 并返回另一个函数。所以它应该真的返回一个普通的装饰器。有点混乱,对吧?我的意思是:
在这里 您可以阅读有关该主题的更多信息 - 也可以使用可调用对象来实现这一点,那里也对此进行了解释。