2012-08-03 28 views
5

我們一直在使用nosetest來運行和收集我們的unittests(這些都是我們喜歡的python unittests)。我們喜歡關於鼻子的東西:nose2 vs py.test與孤立的進程

  • 使用標準的python單元測試(我們喜歡這種結構)。
  • 支持xml報告範圍和測試輸出(用於jenkins)。

我們缺少的是在獨立進程中運行測試的好方法,同時保持良好的錯誤重新生成(我們正在通過python測試C++庫,所以段錯誤不應該是災難性的)。鼻管似乎不再維護,我們有一些問題。

我們正試圖弄清楚我們是否應該 - 修復/使用鼻管 - 切換到nose2並寫入nosepipe2。 - 使用pytest或其他一些測試框架。

我們寧願使用一個良好社區的方法。看起來我們的問題(需要良好隔離的C++插件)可能是一個常見問題,但是使用谷歌搜索我還沒有找到維護的解決方案。來自更有經驗的負責人的建議表示讚賞

回答

13

pytest有xdist plugin它提供了--boxed選項 在受控子進程中運行每個測試。這是一個基本的例子::

# content of test_module.py 

import pytest 
import os 
import time 

# run test function 50 times with different argument 
@pytest.mark.parametrize("arg", range(50)) 
def test_func(arg): 
    time.sleep(0.05) # each tests takes a while 
    if arg % 19 == 0: 
     os.kill(os.getpid(), 15) 

如果你運行這個與::

$ py.test --boxed 
=========================== test session starts ============================ 
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev8 
plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov 
collecting ... collected 50 items 

test_module.py f..................f..................f........... 

================================= FAILURES ================================= 
_______________________________ test_func[0] _______________________________ 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
______________________________ test_func[19] _______________________________ 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
______________________________ test_func[38] _______________________________ 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
=================== 3 failed, 47 passed in 3.41 seconds ==================== 

你會看到一對夫婦的測試報告崩潰,以小寫f表示 和相應的失敗摘要。您還可以使用 的xdist提供的並行化功能,加快您的測試::

$ py.test --boxed -n3 
=========================== test session starts ============================ 
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev8 
plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov 
gw0 I/gw1 I/gw2 I 
gw0 [50]/gw1 [50]/gw2 [50] 

scheduling tests via LoadScheduling 
..f...............f..................f............ 
================================= FAILURES ================================= 
_______________________________ test_func[0] _______________________________ 
[gw0] linux2 -- Python 2.7.3 /home/hpk/venv/1/bin/python 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
______________________________ test_func[19] _______________________________ 
[gw2] linux2 -- Python 2.7.3 /home/hpk/venv/1/bin/python 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
______________________________ test_func[38] _______________________________ 
[gw2] linux2 -- Python 2.7.3 /home/hpk/venv/1/bin/python 
/home/hpk/tmp/doc-exec-420/test_module.py:6: running the test CRASHED with signal 15 
=================== 3 failed, 47 passed in 2.03 seconds ==================== 

原則,只分配到並行的子進程可能會經常即可,避免了啓動每個測試裝箱過程的開銷。如果因爲沒有重新啓動即將到來的測試過程而導致進程數只比-n少,則此功能纔有效。這個限制很可能不需要太多的努力就可以被移除。同時你將不得不使用安全的拳擊選項。

+0

感謝您的幫助示例。它看起來像py.test正是醫生所訂購的。 – jjh 2012-08-06 21:38:59