<small id='xSNpK'></small><noframes id='xSNpK'>

      <tfoot id='xSNpK'></tfoot>
    1. <i id='xSNpK'><tr id='xSNpK'><dt id='xSNpK'><q id='xSNpK'><span id='xSNpK'><b id='xSNpK'><form id='xSNpK'><ins id='xSNpK'></ins><ul id='xSNpK'></ul><sub id='xSNpK'></sub></form><legend id='xSNpK'></legend><bdo id='xSNpK'><pre id='xSNpK'><center id='xSNpK'></center></pre></bdo></b><th id='xSNpK'></th></span></q></dt></tr></i><div id='xSNpK'><tfoot id='xSNpK'></tfoot><dl id='xSNpK'><fieldset id='xSNpK'></fieldset></dl></div>
        <bdo id='xSNpK'></bdo><ul id='xSNpK'></ul>
      <legend id='xSNpK'><style id='xSNpK'><dir id='xSNpK'><q id='xSNpK'></q></dir></style></legend>

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

      时间:2023-09-12
      <legend id='vIqlJ'><style id='vIqlJ'><dir id='vIqlJ'><q id='vIqlJ'></q></dir></style></legend>

        <tbody id='vIqlJ'></tbody>

      <i id='vIqlJ'><tr id='vIqlJ'><dt id='vIqlJ'><q id='vIqlJ'><span id='vIqlJ'><b id='vIqlJ'><form id='vIqlJ'><ins id='vIqlJ'></ins><ul id='vIqlJ'></ul><sub id='vIqlJ'></sub></form><legend id='vIqlJ'></legend><bdo id='vIqlJ'><pre id='vIqlJ'><center id='vIqlJ'></center></pre></bdo></b><th id='vIqlJ'></th></span></q></dt></tr></i><div id='vIqlJ'><tfoot id='vIqlJ'></tfoot><dl id='vIqlJ'><fieldset id='vIqlJ'></fieldset></dl></div>

        <small id='vIqlJ'></small><noframes id='vIqlJ'>

          • <bdo id='vIqlJ'></bdo><ul id='vIqlJ'></ul>

            <tfoot id='vIqlJ'></tfoot>

                本文介绍了Python unittest 将参数传递给父测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                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)
                

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

                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)
                

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

                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)
                

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

                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?

                推荐答案

                我也很难在多个浏览器上运行相同的测试用例.经过多次迭代、反复试验以及朋友们的意见,我为我的项目实施了以下解决方案:

                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:

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

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

                在运行器文件TestSuite.py中:

                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)
                

                这篇关于Python unittest 将参数传递给父测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在window x64上的python中记录selenium webdriver测试 下一篇:如何使用 selenium python 在输入标签中查找和写入

                相关文章

                最新文章

              1. <legend id='7Mu7b'><style id='7Mu7b'><dir id='7Mu7b'><q id='7Mu7b'></q></dir></style></legend>

                  <tfoot id='7Mu7b'></tfoot>
                  1. <i id='7Mu7b'><tr id='7Mu7b'><dt id='7Mu7b'><q id='7Mu7b'><span id='7Mu7b'><b id='7Mu7b'><form id='7Mu7b'><ins id='7Mu7b'></ins><ul id='7Mu7b'></ul><sub id='7Mu7b'></sub></form><legend id='7Mu7b'></legend><bdo id='7Mu7b'><pre id='7Mu7b'><center id='7Mu7b'></center></pre></bdo></b><th id='7Mu7b'></th></span></q></dt></tr></i><div id='7Mu7b'><tfoot id='7Mu7b'></tfoot><dl id='7Mu7b'><fieldset id='7Mu7b'></fieldset></dl></div>
                      <bdo id='7Mu7b'></bdo><ul id='7Mu7b'></ul>

                    <small id='7Mu7b'></small><noframes id='7Mu7b'>