nose2 vs py.test与孤立的进程

jjh*_*jjh 7 python nose pytest

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

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

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

我们试图找出是否应该 - 修复/使用nosepipe - 切换到nose2并写入nosepipe2. - 使用pytest或其他一些测试框架.

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

hpk*_*k42 13

pytest有xdist插件,它提供了--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)
Run Code Online (Sandbox Code Playgroud)

如果你运行这个::

$ 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 ====================
Run Code Online (Sandbox Code Playgroud)

你会看到有几个测试被报告为崩溃,由小写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 ====================
Run Code Online (Sandbox Code Playgroud)

原则上,仅分发到并行子过程通常就足够了,并且避免了为每个测试启动盒装过程的开销.目前只有在崩溃测试的-n次数少于进程数时才会生效,因为重启的测试过程没有重启.可以在不需要太多努力的情况下移除此限制.同时你将不得不使用安全拳击选项.