问题:如何从注入的方法引用decorator中创建的对象/变量?

2024-04-24 07:09:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在decorator中创建的对象的可用性方面遇到了问题,test_case方法需要这些对象。我的代码如下:

def execute_results_navigation(test_case):
    def wrapper(self,*args,**kwargs):
        result=Result()
        pagination=Pagination()
        results_page_index=1
        while results_page_index<=pagination.get_pages_number():

            for results_object_index in range(results.get_objects_number_per_single_page()):
                test_case(self,*args,**kwargs)

                pagination.set_active_page_number(results_page_index)
            results_page_index+=1

    return wrapper

替换test_case方法的是“注入”以下代码(所有操作都使用预定义的修饰器进行):

@execute_results_navigation
def test_check_availability_of_search_results(self):
    """
    test case 2.22
    """
    offer=Offer()

    result.select_hotel(results_caller["button"],results_object_index)
    offer_price=offer.get_offer_object_details().price
    offer.verify_offer_availability(offer_price)
    offer.back_to_search_results()

test_case方法无法访问resultpagination对象和results_object_index变量。调用decorator时,所有对象都已初始化。也许我用这个方法做了一些错误的事情,但是我认为这些实例存在于wrapper方法中,对它们的访问不应该引起问题。你知道吗


Tags: 对象方法testselfgetindexobjectdef
1条回答
网友
1楼 · 发布于 2024-04-24 07:09:42

您将无法在测试用例中访问包装器中定义的局部变量。你知道吗

看起来test\u check\u availability\u of\u search\u results是一个实例方法,所以解决问题的一个方法是将这些变量赋给“self”的属性。你知道吗

相关问题 更多 >