2010-06-24 49 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) 
相關問題