2012-10-25 181 views
2

我試圖在另一個.py文件中調用test_check_footer_visible調用另一個類中的一個類的方法

origin.py有:

class FooterSection(unittest.TestCase): 
    """ 
    """ 
    browser = None 
    site_url = None 

    def setUp(self): 
     self.browser.open(self.site_url) 
     self.browser.window_maximize() 

     # make footer section 
     self._footer = FooterPage(self.browser) 

    def test_check_footer_visible(self): 
     #press the page down 
     self.browser.key_down_native("34") 
     self.browser.key_down_native("34") 
     self.browser.key_down_native("34") 
     print "Page down" 
     time.sleep (10) 

現在dest.py我需要調用test_check_footer_visible()。我怎麼做?

dest.py具有以下格式:

class TestPageErrorSection(unittest.TestCase): 
    """ 
    """ 
    browser = None 
    site_url = None 

    def setUp(self): 
     global SITE_URL, BROWSER 

     if not BROWSER: 
      BROWSER = self.browser 

     if not SITE_URL: 
      SITE_URL = self.site_url 

     BROWSER.open(SITE_URL) 
     BROWSER.window_maximize() 
     print 'page not found 404 error' 
     self._pageerror_section = ErrorSection(BROWSER) 

    def _primary_navigation(self): 
     # I want to call test_check_footer_visible here. 

Call Class Method From Another Class

回答

1

嘗試一切你不能(沒有做一些真正黑幕的東西 - 看到@glglgl評論) - 至少不而不需要改變你的代碼。你不能這樣做的原因是因爲test_check_footer_visible將聲明傳入的第一個元素是類FooterSection(它不是 - 它是TestPageErrorSection的一個實例)的一個實例。你有(至少)一些重選因素的選項。

1)從FooterSectionTestPageErrorSection繼承和直接使用該功能(例如self.test_check_footer_visible()這似乎並不像基於類的名字一個非常好的選擇,我

2)請test_check_footer_visible()常規功能。然後可以從任何一個班級調用它。 3)創建第三個類(例如SectionBase),將test_check_footer_visible放在該類上,並讓其他2個類繼承其中。

+0

不完全 - 它可以通過'FooterSection.test_check_footer_visible.im_func(self)'成爲可能,但它會是非常糟糕的風格 - 有兩個原因:1.因爲它是不好的風格,因爲被調用者旨在從完全不同的階級中獲得「自我」。但它會工作。這只是爲了完整性。 – glglgl

+0

@glglgl - 是的,我想這會奏效。這樣做有點扭曲。從本質上講,這是一個非常可疑的方式實施#2我想...... – mgilson

+0

是的。本質上,它解除了將函數作爲方法所做的過程...'FooterSection .__ dict __ ['test_check_footer_visible'](self)'也可以,但現在變得很奇怪。 – glglgl