【问题标题】:How can I execute in parallel Selenium Python tests with unittest如何使用 unittest 并行执行 Selenium Python 测试
【发布时间】:2016-07-04 17:02:49
【问题描述】:

例如我有两个测试:

class Test(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.get("http://google.com")

def tearDown(self):
    self.driver.quit()

def test_selenium_1(self):
    search_field = self.driver.find_element_by_id("lst-ib")
    search_field.send_keys("Test 1. Number 1")
    search_field.submit()
    time.sleep(2)

def test_selenium_2(self):
    search_field = self.driver.find_element_by_id("lst-ib")
    search_field.send_keys("Test 1. Number 2")
    search_field.submit()
    time.sleep(2)

if __name__ == '__main__':
    unittest.main()

如何使用concurrent.futures.Executor 并行执行这两个测试?

有可能吗?

【问题讨论】:

  • 有没有没有nose、pytest等的方法可以做到。我想找到用unittest的方法。
  • nose 多进程代码是开源的(他们在他们的网站上列出了所有代码)并且可以提供很多关于如何实现并行测试的见解
  • 在这个具体的例子中,如果这些测试并行执行,看起来你会有一个竞争条件......
  • 我执行了大约 40 次测试,一切正常。你为什么决定?也许我应该解决一些问题

标签: python unit-testing selenium parallel-processing


【解决方案1】:

我为此目的创建了 Runner。

现在我可以通过模块、类和方法并行执行测试。

import unittest
from concurrent.futures import ThreadPoolExecutor

class Runner():

    def parallel_execution(self, *name, options='by_module'):

        """
        name - name of the class with tests or module with classes that contain tests
        modules - name of the module with tests or with class that contains tests
        options:
            by_method - gather all tests methods in the class/classes and execute in parallel
            by_module - gather all tests from modules in parallel
            by_class - will execute all classes (with tests) in parallel
        """

        suite = unittest.TestSuite()

        if (options=='by_method'):
            for object in name:
                for method in dir(object):
                    if (method.startswith('test')):
                        suite.addTest(object(method))
        elif (options=='by_class'):
            for object in name:
                suite.addTest(unittest.TestLoader().loadTestsFromTestCase(object))

        elif (options=='by_module'):
            for module in name:
                suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
        else:
            raise ValueError("Parameter 'options' is incorrect."
                             "Available options: 'by_method', 'by_class', 'by_module'")

        with ThreadPoolExecutor(max_workers=10) as executor:
            list_of_suites = list(suite)
            for test in range(len(list_of_suites)):
                test_name = str(list_of_suites[test])
                executor.submit(unittest.TextTestRunner().run, list_of_suites[test])

示例:

#by_methods
Runner().parallel_execution(Test1.Test1, Test2.Test22, Test2.Test33, options='by_method')

#by_class
Runner().parallel_execution(Test1.Test1, Test2.Test22, Test2.Test33, options='by_class')

#by_modules
Runner().parallel_execution(Test1, Test2)

【讨论】:

  • 我面临同样的问题,你的解决方案对测试用例运行良好,但我也在使用 before 和 after 方法,它们没有被调用,只有测试用例被执行
  • 您是否解决了使用此解决方案对测试运行设置/拆卸的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
相关资源
最近更新 更多