为什么我的自我实例没有在方法中传递

时间:2019-11-30 06:16:54

标签: python python-3.x

我有以下代码。 ResourceManagerSuspension类是从TestCase类继承的。 TestCase按字母顺序执行测试,如testA的方法将在testB之前执行。 因此,testLinkData方法在testSuspension和self.link在testLinkData中获取其值之前执行。 我正在初始化变量“ self.link”,并在执行方法testSuspension时调用getQueryValues。

我的问题是为什么

“ self.link”没有在getQueryValues方法中传递? 谁能解释自我机制在这里的运作方式,也许我做错了什么?

class ResourceManagerSuspension(TestCase):
    @classmethod
    def setUpClass(self):
        logger.info("=== Starting setup ===")
       # self.rm_obj = ResourceManager(agg='mapper-prefix1-aggs.A.m2-test.akamai.com')
        self.rm_obj = ResourceManager()
        self.rm_leader = self.rm_obj.get_rm_leader() 
        logger.info("RM lead target is %s" % (self.rm_leader))
        self.found = "" 
        self.link = ""  
        logger.info("self.link is : {}".format(self.link))
        logger.info("self in setUpClass is : {}".format(self.__dict__))         

    #the name should be get link number to get started etc
    def testLinkData(self):
        linkValues = {} 
        #get a random link  
        sqlquery = "select * from rm_links_debugonly where adjuster_reason not like '\%suspend\%' and ip=" + self.rm_leader + " and link!=0 limit 1"
        link_obj = self.rm_obj.get_link_info(query=sqlquery) 
        for row in link_obj:
            self.link = row.link
        self.getDynamicConfig()
        logger.info("self.link is : {}".format(self.link))
        logger.info("self in testLinkData is : {}".format(self.__dict__))

    def testSuspension(self):
        if not ResourceManagerSuspension.found:
            #get the 'control_reason' from "rm_link_load_control_debugonly" and 'adjuster_cap' from "rm_links_debugonly" before submitting the dynamic config 
            self.control_reason_without_config, self.adjuster_cap_without_config = self.getQueryValues()

            logger.info("param not present in the file, submitting with the param")
            self.rm_obj.dyamic_config_submit(fromLocation = self.rm_obj.dynamic_config_modified, to = self.rm_obj.dynamic_config_incoming)


       else:
           logger.info("param is already present, removing it and submitting the config")
           self.rm_obj.dyamic_config_submit(fromLocation = self.rm_obj.dynamic_config_modified, to = self.rm_obj.dynamic_config_incoming)
          logger.info("self.link is : {}".format(self.link))
          logger.info("self in testSuspension is : {}".format(self.__dict__))

    def getQueryValues(self):
       logger.info("self in getQueryValues is : {}".format(self.__dict__))
       logger.info("self.link is : {}".format(self.link))

代码最后一行的输出是

[05:55:39.709 test_suspension_2:  61  I] self.link is : 

1 个答案:

答案 0 :(得分:0)

单元测试应该能够彼此独立运行,这意味着getQueryValues应该可以在testLinkData之前或之后运行,但是在您的实现中,getQueryValues必须运行在testLinkData之后,以便获得期望的输出。

要解决此问题,您必须编写测试方法可以在运行时调用的设置实用程序方法,即为self.link提供一个独立于另一个测试的值。