I have a parent test class named as basetestcase() This is inherited by all the test classes
class BaseTestCase(unittest.TestCase):
driver = None
browser = read from command line
operatingSystem = read from command line
url = read from command line
@classmethod
def setUpClass(cls):
"""
SetUp to initialize webdriver session, pages and other needed objects
Returns:
None
"""
# Get webdriver instance
# Browser should be read from the arguments
if browser == "iexplorer":
cls.driver = webdriver.Ie()
elif browser == "firefox":
cls.driver = webdriver.Firefox()
elif browser == "chrome":
cls.driver = webdriver.Chrome()
else:
cls.driver = webdriver.PhantomJS()
# Similarly I want to get operating system and url also from command line
driver.get(url)
print("Tests are running on: " + operatingSystem)
Then I have two separate test classes:
class TestClass1(BaseTestCase):
@classmethod
def setUpClass(cls):
super(TestClass1, cls).setUpClass()
# Create object of another class to use in the test class
# cls.abc = ABC()
def test_methodA(self):
# self.abc.methodFromABC() # This does not work
# Not sure if I can use self.driver as it was defined as cls.driver in the setUpClass()
self.driver.find_element(By.ID, "test_id").click()
if __name__ == '__main__':
unittest.main(verbosity=2)
This is the 2nd class, both the classes are in separate .py files
class TestClass2(GUIBaseTestCase):
@classmethod
def setUpClass(self):
super(TestClass2, self).setUpClass()
def test_methodA(self):
self.driver.find_element(By.ID, "test_id").click()
if __name__ == '__main__':
unittest.main(verbosity=2)
Then I have a test suite script, a separate .py file which clubs them together to run in a suite
import unittest
from tests.TestClass1 import TestClass1
from tests.TestClass2 import TestClass2
# Get all tests from TestClass1 and TestClass2
tc1 = unittest.TestLoader().loadTestsFromTestCase(TestClass1)
tc2 = unittest.TestLoader().loadTestsFromTestCase(TestClass2)
# Create a test suite combining TestClass1 and TestClass2
smokeTest = unittest.TestSuite([tc1, tc2])
unittest.TextTestRunner(verbosity=2).run(smokeTest)
I want to run the test suite and want to provide browser, operating system and url to the basetestcase from the command line and these arguments are directly used by basetestcase.py. Actual test classes inherit the basetestcase.
Could you please help me with how to get these values from the command line in the best way and provide to the basetestcase?