2011-07-22 195 views
1

我編寫了一個使用Unit testing framework的GUI應用程序。 該應用程序在樹視圖中顯示所有找到的單元測試。 如果您單擊單元測試名稱,程序將運行此測試並顯示結果。 它的工作原理!單元測試框架:在新進程中運行測試

但是現在我希望每個測試用例都運行在一個新的進程中。 我創建了一個「工作線程」(帶有模塊線程),但是此線程也在同一進程中運行。

模塊「multiprocessing」是解決方案,但我遇到了問題,我的實施。 我把我的「測試執行代碼」在自己的功能和嘗試:

item = self.GetSelection() 
name = self.GetItemText(item) 
p = multiprocessing.Process(target=ExecuteTest, args=(name, item)) 
p.daemon = True 
p.start() 

但後來我得到了錯誤。

Traceback (most recent call last): 
    File "C:\a\b\c\hhh\a.py", line 577, in OnLeftDClick 
    p.start() 
    File "C:\Python26\lib\multiprocessing\process.py", line 104, in start 
    self._popen = Popen(self) 
    File "C:\Python26\lib\multiprocessing\forking.py", line 239, in __init__ 
    dump(process_obj, to_child, HIGHEST_PROTOCOL) 
    File "C:\Python26\lib\multiprocessing\forking.py", line 162, in dump 
    ForkingPickler(file, protocol).dump(obj) 
    File "C:\Python26\lib\pickle.py", line 224, in dump 
    self.save(obj) 
    File "C:\Python26\lib\pickle.py", line 331, in save 
    self.save_reduce(obj=obj, *rv) 
    File "C:\Python26\lib\pickle.py", line 419, in save_reduce 
    save(state) 
    File "C:\Python26\lib\pickle.py", line 286, in save 
    f(self, obj) # Call unbound method with explicit self 
    File "C:\Python26\lib\pickle.py", line 649, in save_dict 
    self._batch_setitems(obj.iteritems()) 
    File "C:\Python26\lib\pickle.py", line 681, in _batch_setitems 
    save(v) 
    File "C:\Python26\lib\pickle.py", line 286, in save 
    f(self, obj) # Call unbound method with explicit self 
    File "C:\Python26\lib\pickle.py", line 548, in save_tuple 
    save(element) 
    File "C:\Python26\lib\pickle.py", line 331, in save 
    self.save_reduce(obj=obj, *rv) 
    File "C:\Python26\lib\pickle.py", line 419, in save_reduce 
    save(state) 
    File "C:\Python26\lib\pickle.py", line 286, in save 
    f(self, obj) # Call unbound method with explicit self 
    File "C:\Python26\lib\pickle.py", line 649, in save_dict 
    self._batch_setitems(obj.iteritems()) 
    File "C:\Python26\lib\pickle.py", line 686, in _batch_setitems 
    save(v) 
    File "C:\Python26\lib\pickle.py", line 331, in save 
    self.save_reduce(obj=obj, *rv) 
    File "C:\Python26\lib\pickle.py", line 396, in save_reduce 
    save(cls) 
    File "C:\Python26\lib\pickle.py", line 286, in save 
    f(self, obj) # Call unbound method with explicit self 
    File "C:\Python26\lib\pickle.py", line 748, in save_global 
    (obj, module, name)) 
pickle.PicklingError: Can't pickle <type 'PySwigObject'>: it's not found as __builtin__.PySwigObject 

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "C:\Python26\lib\multiprocessing\forking.py", line 342, in main 
    self = load(from_parent) 
    File "C:\Python26\lib\pickle.py", line 1370, in load 
    return Unpickler(file).load() 
    File "C:\Python26\lib\pickle.py", line 858, in load 
    dispatch[key](self) 
    File "C:\Python26\lib\pickle.py", line 880, in load_eof 
    raise EOFError 
EOFError 

有沒有人有什麼想法?

回答

1

顯然,你試圖「醃製」一些不可取的東西(似乎是一個wx對象)。嘗試僅用作參數「簡單」對象(即沒有圖形對象),它應該工作得更好。 您也可以看看python nose unitest庫,它允許您自動運行多進程中的測試。

-1

使用fork產生子進程。這將需要你的一些工作,但會起作用。

作爲說明:multiprocessnig模塊是使用fork實現的。


更新:你的代碼仔細看後似乎item viariable不與pickle(就是一個字?)。我不認爲你需要它,因爲你可以用簡單的東西(一個值或某種類型)代替它。

+0

「pickleable(這是一個詞?)」,那麼'pythonic'呢?或'voooooooooooom' – SingleNegationElimination

0

我建議您創建一個運行測試的單獨腳本,然後您可以使用subprocess模塊調用該腳本,併爲其提供要運行的測試的名稱。

好處是,您可以在持續集成服務器,每晚構建或其他不需要GUI的批處理類型進程中使用同一腳本。

相關問題