2015-12-20 141 views
1

我想获得一些帮助,以使用多处理模块并行运行多个python测试用例。我创建了一个FooTest类,其中包含10个测试用例(testA,testB ...)。在课堂外有一个测试用例test_foo。并行运行测试用例python

如何使用python多处理模块并行运行所有测试用例?谢谢您的帮助。

import unittest 
import time 

def setUp(): 
    print "module setup" 

def test_foo(): 
    pass 

class FooTest(unittest.TestCase): 

    # preparing to test 
    def setUp(self): 
     """ Setting up for the test """ 
     print "FooTest:setUp_:begin" 

    # ending the test 
    def tearDown(self): 
     """Cleaning up after the test""" 
     print "FooTest:tearDown_:begin" 

    # test routine A 
    def testA(self): 
     """Test routine A""" 
     print "FooTest:testA" 
     time.sleep(2) 

    # test routine B 
    def testB(self): 
     """Test routine B""" 
     print "FooTest:testB" 

    # test routine C 
    def testC(self): 
     """Test routine C""" 
     print "FooTest:testC" 

    # test routine D 
    def testD(self): 
     """Test routine D""" 
     print "FooTest:testD" 

    # test routine E 
    def testE(self): 
     """Test routine E""" 
     print "FooTest:testE" 
     time.sleep(2) 

    # test routine F 
    def testF(self): 
     """Test routine F""" 
     print "FooTest:testF" 

    # test routine G 
    def testG(self): 
     """Test routine G""" 
     print "FooTest:testG" 

    # test routine H 
    def testH(self): 
     """Test routine H""" 
     print "FooTest:testH" 

    # test routine I 
    def testI(self): 
     """Test routine I""" 
     print "FooTest:testI" 

    # test routine J 
    def testJ(self): 
     """Test routine J""" 
     print "FooTest:testJ" 
     time.sleep(2) 

回答

2

根据nose documentation,您还可以通过运行nosetests --help得到:这么多的过程中运行

--processes = NUM​​传播测试。设置为 a 的数字等于您的设备中处理器或内核的数量为 以获得最佳效果。传递负数 以使进程数自动设置为 核心数。通过0意味着禁用 并行测试。除非设置了NOSE_PROCESSES ,否则缺省值为0。 [NOSE_PROCESSES]

因此,只需运行nosetests --processes=-1即可使用计算机上的所有核心并行运行测试。

+0

谢谢你的回答。有效。 – Selva