隔离进程中的nose2 vs py.test [英] nose2 vs py.test with isolated processes

查看:134
本文介绍了隔离进程中的nose2 vs py.test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在使用鼻子测试来运行和收集我们的单元测试(所有单元测试都编写为我们喜欢的python单元测试).我们喜欢鼻子的事情:

We have been using nosetest for running and collecting our unittests (which are all written as python unittests which we like). Things we like about nose:

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

我们缺少的是一种在隔离的进程中运行测试并保持良好的错误报错的好方法(我们正在通过python测试C ++库,因此segfaults不会造成灾难性的影响).鼻管似乎不再维护,我们对此有一些疑问.

What we are missing is a good way to run tests in isolated processes while maintaining good error repoorting (we are testing C++ libraries through python so segfaults should not be catastrophic). nosepipe seems to be no longer maintained and we have some problems with it.

我们正在尝试确定是否应该 -修复/使用鼻管 -切换到"nose2"并编写"nosepipe2". -使用pytest或其他测试框架.

We are trying to figure out whether we should - fix/use nosepipe - switch to nose2 and write nosepipe2. - use pytest or some other testing framework.

我们宁愿使用拥有良好社区的方法.看来我们的问题(需要良好隔离的C ++插件)可能是一个常见问题,但在Google搜索中我没有找到可以维护的解决方案.感谢经验丰富的负责人的建议.

We would prefer to use an approach with a good community. It seems our problem (C++ plugins requiring good isolation) might be a common problem but googling I have not found solutions that are maintained. Advice from more experienced heads appreciated.

推荐答案

pytest具有 xdist插件,该插件提供--boxed选项 在受控子流程中运行每个测试.这是一个基本示例:

pytest has the xdist plugin which provides the --boxed option to run each test in a controlled subprocess. Here is a basic example::

# 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提供的并行化功能可加快您的测试速度::

You'll see that a couple of tests are reported as crashing, indicated by lower-case f and the respective failure summary. You can also use the xdist-provided parallelization feature to speed up your testing::

$ 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进程数量的情况下才有效,因为垂死的测试进程不会重新启动.无需过多努力就可以消除此限制.同时,您将必须使用安全拳击选项.

In principle, just distributing to parallel subprocesses may often suffice and avoids the overhead of starting a boxed process for each test. This currently only works if you only have less crashing tests than the -n number of processes because a dying testing process is not restarted. This limitation could probably be removed without too much effort. Meanwhile you will have to use the safe boxing option.

这篇关于隔离进程中的nose2 vs py.test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆