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?
If you have multiple classes then you can declare the Web Driver as static and you can use the same reference in other classes too. If you are using BeforeTest/BeforeSuite, then create instance in this annotation and use the same through out the tests.
Question2: WebDriver is an interface, then can we create an object instance of an interface? Yes, you can create concrete instances that implement an interface. In fact, to use any interface there must be at least one concrete implementation. Save this answer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With