2

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

Community
  • 1
  • 1
Anuradha
  • 31
  • 1
  • 4

1 Answers1

1

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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Not exactly - it can be made possible with `FooterSection.test_check_footer_visible.im_func(self)`, but it would be VERY bad style - for 2 reasons: 1. because it is bad style as such and 2. because the callee is intended to get a `self` from a completely different class. But it would work. This just for completeness. – glglgl Oct 25 '12 at 13:02
  • @glglgl -- Yes, I suppose that would work. It's a little twisted to do it that way. Essentially, that is a very shady way of implementing #2 I suppose ... – mgilson Oct 25 '12 at 13:12
  • Yes. Essentially, it undoes the process done by making the function a method... `FooterSection.__dict__['test_check_footer_visible'](self)` would work as well, but now it gets weird. – glglgl Oct 25 '12 at 13:14
  • @glglgl I implemented FooterSection.test_check_footer_visible.im_func(self) - it worked. Thanks. – Anuradha Nov 06 '12 at 11:37
  • I have one more question though: Let us assume that in origin.py I have def testing_again(self): #call test_check_footer_visible() And in dest.py I have the lines FooterSection.testing_again.im_func(self) it does not work. – Anuradha Nov 06 '12 at 11:43
  • @Anuradha -- how did you import origin? if you do `from origin import FooterSection` in dest.py, I would expect it to work. If you just did `import origin`, you'd need `origin.FooterSection.testing_again.im_func(self)`. – mgilson Nov 06 '12 at 12:49
  • @mgilson...Thanks. I did from origin import FooterSection.. but unfortunately it did not work – Anuradha Nov 07 '12 at 06:12