Anuradha
Anuradha

Reputation: 31

Calling a method of one class in another class

I'm attempting to call test_check_footer_visible in another .py file.

origin.py has:

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)

Now in dest.py I need to call test_check_footer_visible(). How do I do that?

dest.py has the following format:

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.

I tried everything in Call Class Method From Another Class

Upvotes: 2

Views: 405

Answers (1)

mgilson
mgilson

Reputation: 310187

You can't (without doing some really shady things -- see comments by @glglgl) -- at least not without changing your code somewhat. The reason you can't is because test_check_footer_visible will assert that the first element passed in is an instance of the class FooterSection (which it isn't -- it's an instance of TestPageErrorSection). You have (at least) a couple of options for the re-factor.

1) Make TestPageErrorSection inherit from FooterSection and use that function directly (e.g. self.test_check_footer_visible() This doesn't seem like a very good option to me based on the class names

2) Make test_check_footer_visible() a regular function. It can then be called from either class.

3) Create a 3rd class (e.g. SectionBase), put test_check_footer_visible on that class and have your other 2 classes inherit from it.

Upvotes: 1

Related Questions