Python unittest passing arguments to parent test class
Asked Answered
P

1

1

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?

Plethora answered 7/6, 2016 at 8:55 Comment(0)
S
0

I also struggled to run the same test cases on multiple browsers. After a lot of iterations, trial and error and input from friends I implemented the following solutions to my projects:

Here TestCase is the class that has all the tests and the browser driver is None. SafariTestCase inherits the TestCase and overrides setUpClass and sets the browser driver to be safari driver and same with the ChromeTestCase and you can add more class for other browsers. Command Line input can be taken in the TestSuite file and conditionally load tests based on the arguments:

class TestCase(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    cls.browser = None

  def test_1(self):
    self.assert(self.browser.find_element_by_id('test1')

  def test_2(self):
    self.assert(self.browser.find_element_by_id('test2')

  def test_3(self):
    self.assert(self.browser.find_element_by_id('test3')

  @classmethod
  def tearDownClass(cls):
    cls.browser.quit()


class SafariTestCase(TestCase):
  @classmethod:
  def setUpClass(cls):
    cls.browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')

class ChromeTestCase(TestCase):
  @classmethod:
  def setUpClass(cls):
    cls.browser = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')  

In the runner file TestSuite.py:

import TestCase as tc

if len(sys.argv) == 1:
  print("Missing arguments, tell a couple of browsers to test against.")
  sys.exit(1)

if sys.argv[1] == 'safari':
  test = unittest.TestLoader().loadTestsFromTestCase(tc.SafariTestCase)

if sys.argv[1] == 'chrome':
  test = unittest.TestLoader().loadTestsFromTestCase(lt.ChromeTestCase)

unittest.TextTestRunner().run(test)
Similar answered 14/3, 2019 at 12:26 Comment(3)
How do you run that? Also, there can be more than one class with test cases. How do you handle that?Plethora
The test case files will contain all the browser specific drivers and you import add them to the testSuite with 'addTest' function. Run the TestSuite with 'python TestCase.py safari' or 'python TestCase.py chrome'. You can play with threads as well. I know the driver part is bit repetitive but at least the test cases are not. I am still trying to find solution to keep the drives in 1 file. I might try mulit inheritence. May be if we discuss more on this we can find a optimal solution. Thanks for checking out and appreciate your comment which is valid.Similar
Thanks for the response. I am also working on putting a complete framework.Plethora

© 2022 - 2024 — McMap. All rights reserved.