2010-06-24 51 views
7

我有一个包含很多.py文件(比如test_1.py,test_2.py等等)的目录。它们中的每一个都被正确编写以便与鼻子一起使用。所以当我运行nosetests脚本时,它会找到所有.py文件中的所有测试并执行它们。在Python中使用鼻子进行并行化测试

我现在想要对它们进行并行化处理,以便所有.py文件中的所有测试都被视为可并行化并委派给工作进程。

似乎在默认情况下,这样做的:

nosetests --processes=2 

没有引入并行的一切,所有的.py文件中的所有测试还是在短短的一个过程

我试图把一个_multiprocess_can_split_ = true在运行每个.py文件但这并没有什么区别

感谢您的任何意见!

+0

你是如何确定的一个补充睡眠是否并行化呢?我有一个类似的目标,但我*认为*我有一个与你不同的问题...可能不是,但。 – fholo 2010-09-10 17:29:32

回答

12

看来,鼻子,实际上是多进程插件,将使测试并行运行。需要注意的是,它的工作方式,最终可能不会对多个进程执行测试。该插件创建一个测试队列,产生多个进程,然后每个进程并发地使用该队列。每个进程都没有测试调度,因此如果你的测试执行得非常快,他们最终可能会在同一个进程中执行。

以下示例显示本beaviour:

文件test1.py

import os 
import unittest 

class testProcess2(unittest.TestCase): 

    def test_Dummy2(self): 
     self.assertEqual(0, os.getpid()) 

文件test2.py

import os 
import unittest 

class testProcess2(unittest.TestCase): 

    def test_Dummy2(self): 
     self.assertEqual(0, os.getpid()) 

运行nosetests --processes = 2个输出(注意相同的工艺ID)

FF 
====================================================================== 
FAIL: test_Dummy2 (test1.testProcess2) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\temp\test1.py", line 7, in test_Dummy2 
    self.assertEqual(0, os.getpid()) 
AssertionError: 0 != 94048 

====================================================================== 
FAIL: test_Dummy1 (test2.testProcess1) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\temp\test2.py", line 8, in test_Dummy1 
    self.assertEqual(0, os.getpid()) 
AssertionError: 0 != 94048 

---------------------------------------------------------------------- 
Ran 2 tests in 0.579s 

FAILED (failures=2) 

现在,如果我们在测试

import os 
import unittest 
import time 

class testProcess2(unittest.TestCase): 

    def test_Dummy2(self): 
     time.sleep(1) 
     self.assertEqual(0, os.getpid()) 

我们得到了(注意,不同的进程ID)

FF 
====================================================================== 
FAIL: test_Dummy1 (test2.testProcess1) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\temp\test2.py", line 8, in test_Dummy1 
    self.assertEqual(0, os.getpid()) 
AssertionError: 0 != 80404 

====================================================================== 
FAIL: test_Dummy2 (test1.testProcess2) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\temp\test1.py", line 10, in test_Dummy2 
    self.assertEqual(0, os.getpid()) 
AssertionError: 0 != 92744 

---------------------------------------------------------------------- 
Ran 2 tests in 1.422s 

FAILED (failures=2) 
相关问题