如何使用unittest并行执行Selenium Python测试

时间:2016-07-04 17:02:49

标签: python unit-testing selenium parallel-processing

例如,我有两个测试:

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并行执行这两项测试?

有可能吗?

1 个答案:

答案 0 :(得分: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)