我正在尝试在另一个 .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 中尝试了一切
请您参考如下方法:
您不能(如果不做一些非常阴暗的事情——请参阅@glglgl 的评论)——至少在不稍微更改代码的情况下不能。你不能的原因是因为 test_check_footer_visible 会断言传入的第一个元素是 FooterSection 类的一个实例(它不是 - 它是TestPageErrorSection)。您(至少)有几个重构选项。
1) 使 TestPageErrorSection 继承自 FooterSection 并直接使用该函数(例如 self.test_check_footer_visible() 这不会根据类名,对我来说似乎是一个很好的选择
2) 使 test_check_footer_visible() 成为常规函数。然后可以从任一类中调用它。
3) 创建第 3 个类(例如 SectionBase),将 test_check_footer_visible 放在该类上,让您的其他 2 个类继承它。
