user1076879
user1076879

Reputation: 55

How can I share one webdriver instance in my test classes in the suite? I use Selenium2 and Python

My code is like this:

class class1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def testListRolesTitle(self):
        driver=self.driver
        driver.get("www.google.com")

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
        asert...


class class2(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def testListRolesTitle(self):
        driver=self.driver
        driver.get("www.google.com")
        assert...

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

def suite():
    s1 = unittest.TestLoader().loadTestsFromTestCase(class1)
    s2 = unittest.TestLoader().loadTestsFromTestCase(class2)

    return unittest.TestSuite([s1,s2])

if __name__ == "__main__":

    run(suite())

When I ran the suite both of the test classes started a new firefox instance in setup methord. My question is if it's possible to make the two test classed use the same firefox instance? I don't want to put them together in one class.

Any ideas?

Upvotes: 6

Views: 4617

Answers (1)

jcollado
jcollado

Reputation: 40424

You can have a setup function that applies to the whole module instead of just to the class as explained here.

In your case, that would be something like:

def setUpModule():
    DRIVER = webdriver.Firefox()

def tearDownModule():
    DRIVER.quit()

Note that DRIVER is a global variable in this case so that it's available to the objects of all classes.

Also, note that test case ordering might cause that your module setup functions are called multiple times as explained in the documentation:

The default ordering of tests created by the unittest test loaders is to group all tests from the same modules and classes together. This will lead to setUpClass / setUpModule (etc) being called exactly once per class and module. If you randomize the order, so that tests from different modules and classes are adjacent to each other, then these shared fixture functions may be called multiple times in a single test run.

It think this example should make clear when each setup method/function is executed:

import unittest

def setUpModule():
    print 'Module setup...'

def tearDownModule():
    print 'Module teardown...'

class Test(unittest.TestCase):
    def setUp(self):
        print 'Class setup...'

    def tearDown(self):
        print 'Class teardown...'

    def test_one(self):
        print 'One'

    def test_two(self):
        print 'Two'

The output from this is:

$ python -m unittest my_test.py

Module setup...
Class setup...
One
Class teardown...
.Class setup...
Two
Class teardown...
.Module teardown...

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Upvotes: 3

Related Questions