0

I would like to do something like this:

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Chrome()
        
        # Login User
        self.driver.get(self.live_server_url + '/somesite/login/')
        self.driver.find_element(By.ID, "username").click()
        self.driver.find_element(By.ID, "username").send_keys("foo")
        self.driver.find_element(By.ID, "password").click()
        self.driver.find_element(By.ID, "password").send_keys("bar")
        self.driver.find_element(By.ID, "login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDown(self):
        self.driver.quit()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page

I just feel it is inefficient to call setUp() on every test case.

Any help would be appreciated, thanks...

kaskenades
  • 25
  • 6

2 Answers2

0

so move setUp from TestSuite to LiveServerTestCase class

olli_kahn
  • 157
  • 6
  • 1
    to improve the answer, please give some explanation as to reason that this fixes the problem – Kirby Sep 03 '20 at 23:53
  • thank you @olli_kahn for your input. I don't want to mess with third-party modules, but if I understand you correctly, by extending `LiveServerTestCase`; it should work. So would `super()` work in this case? If so, how would I implement it? – kaskenades Sep 04 '20 at 15:01
0

I hate to answer my own question... but I got it working thanks to olli_kahn's hint - extending LiveServerTestCase.

Steps:

  • Use setUpClass/tearDownClass (not setUp/tearDown)
  • Declare setUpClass/tearDownClass as @classmethods (put @classmethod first if using more decorators)
  • Use super() to extend LiveServerTestCase
  • Use cls instead of self for @classmethod's to conform to PEP8

Complete code:

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        super(TestSuite, cls).setUpClass()

        cls.driver = webdriver.Chrome()
        
        # Login User
        cls.driver.get(cls.live_server_url + '/somesite/login/')
        cls.driver.find_element(By.ID, "username").click()
        cls.driver.find_element(By.ID, "username").send_keys("foo")
        cls.driver.find_element(By.ID, "password").click()
        cls.driver.find_element(By.ID, "password").send_keys("bar")
        cls.driver.find_element(By.ID, "login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(TestSuite, cls).tearDownClass()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page
kaskenades
  • 25
  • 6