ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python unittest将参数传递给父测试类

2019-10-13 04:58:23  阅读:251  来源: 互联网

标签:python-unittest python python-3-x selenium-webdriver automated-tests


我有一个名为basetestcase()的父测试类
这是所有测试类所继承的

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)

然后,我有两个单独的测试类:

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)

这是第二个类,两个类都位于单独的.py文件中

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)

然后我有一个测试套件脚本,一个单独的.py文件,将它们组合在一起以在套件中运行

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)

我想运行测试套件,并希望从命令行提供浏览器,操作系统和url到basetestcase,这些参数直接由basetestcase.py使用.
实际的测试类继承了basetestcase.

您能帮助我如何以最佳方式从命令行获取这些值并将其提供给basetestcase吗?

解决方法:

我还努力在多个浏览器上运行相同的测试用例.经过多次反复,反复试验和朋友的反馈,我为我的项目实现了以下解决方案:

这里的TestCase是具有所有测试的类,浏览器驱动程序为None. SafariTestCase继承了TestCase并覆盖setUpClass并将浏览器驱动程序设置为safari驱动程序,与ChromeTestCase相同,并且您可以为其他浏览器添加更多类.命令行输入可以在TestSuite文件中获取,并根据以下参数有条件地加载测试:

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')  

在运行程序文件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)

标签:python-unittest,python,python-3-x,selenium-webdriver,automated-tests
来源: https://codeday.me/bug/20191013/1905638.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有