nose2 vs py.test与孤立的进程

时间:2012-08-03 19:46:33

标签: python nose pytest

我们一直在使用nosetest来运行和收集我们的单元测试(它们都写成我们喜欢的python单元测试)。我们喜欢鼻子的事情:

  • 使用标准的python单元测试(我们喜欢这种结构)。
  • 支持在xml中报告覆盖率和测试输出(对于jenkins)。

我们缺少的是在隔离进程中运行测试的好方法,同时保持良好的错误重新排序(我们通过python测试C ++库,因此段错误不应该是灾难性的)。鼻烟管似乎不再维护,我们遇到了一些问题。

我们正试图弄清楚我们是否应该这样做 - 修理/使用鼻烟管 - 切换到nose2并写入nosepipe2。 - 使用pytest或其他一些测试框架。

我们更愿意使用一个良好社区的方法。看来我们的问题(C ++插件需要良好的隔离)可能是一个常见的问题,但谷歌搜索我没有找到维护的解决方案。来自更有经验的负责人的建议表示赞赏。

1 个答案:

答案 0 :(得分: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进程数时才有效,因为未重新启动死亡测试进程。可以在不需要太多努力的情况下移除此限制。同时你必须使用安全拳击选项。