py.test:如何自动检测子进程中的异常?

时间:2012-11-15 15:20:29

标签: python testing multiprocessing pytest

我正在Linux上运行py.test,因为模块会大量使用多处理。子进程中的异常不会被检测为错误。示例测试文件pytest_mp_test.py

import multiprocessing

def test_mp():
    p = multiprocessing.Process(target=child)
    p.start()
    p.join()

def child():
    assert False

执行:

$ py.test pytest_mp_test.py 
================================== test session starts ===================================
platform linux2 -- Python 2.7.3 -- pytest-2.3.3
plugins: cov
collected 1 items 

pytest_mp_test.py .

================================ 1 passed in 0.04 seconds ================================

未检测到错误。使用-s

调用时会打印例外
$ py.test -s pytest_mp_test.py 
================================== test session starts ===================================
platform linux2 -- Python 2.7.3 -- pytest-2.3.3
plugins: cov
collected 1 items 

pytest_mp_test.py Process Process-1:
Traceback (most recent call last):
  File "/apps11/bioinfp/Python-2.7.3/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/apps11/bioinfp/Python-2.7.3/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/bioinfp/jang/hobbyproggorn/gevent-messagepipe/gevent-messagepipe/pytest_mp_test.py", line 9, in child
    assert False
AssertionError: assert False
.

================================ 1 passed in 0.04 seconds ================================

当我手动查看测试日志时,我意识到何时出现问题。但是,我想知道是否有一种利用py.test自动化异常检测的方法。

我必须验证孩子在父母的退出代码吗?这是唯一的方法吗?

2 个答案:

答案 0 :(得分:5)

经过一些进一步的考虑后,我认为只是在孩子中捕获预期的异常并检查孩子的退出状态是一个非常干净和可靠的解决方案,它不会在测试中添加额外的IPC组件。示例代码:

import multiprocessing
from py.test import raises

class CustomError(Exception):
    pass

def test_mp_expected_fail():
    p = multiprocessing.Process(target=child_expected_fail)
    p.start()
    p.join()
    assert not p.exitcode

def test_mp_success():
    p = multiprocessing.Process(target=child)
    p.start()
    p.join()
    assert not p.exitcode

def test_mp_unexpected_fail():
    p = multiprocessing.Process(target=child_unexpected_fail)
    p.start()
    p.join()
    assert not p.exitcode


def child_expected_fail():
    with raises(CustomError):
        raise CustomError

def child_unexpected_fail():
    raise TypeError

def child():
    pass

执行:

$ py.test pytest_mp_test.py 
================================== test session starts ===================================
platform linux2 -- Python 2.7.3 -- pytest-2.3.3
plugins: cov
collected 3 items 

pytest_mp_test.py ..F

======================================== FAILURES ========================================
________________________________ test_mp_unexpected_fail _________________________________

    def test_mp_unexpected_fail():
        p = multiprocessing.Process(target=child_unexpected_fail)
        p.start()
        p.join()
>       assert not p.exitcode
E       assert not 1
E        +  where 1 = <Process(Process-3, stopped[1])>.exitcode

pytest_mp_test.py:23: AssertionError
------------------------------------ Captured stderr -------------------------------------
Process Process-3:
Traceback (most recent call last):
  File "/apps11/bioinfp/Python-2.7.3/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/apps11/bioinfp/Python-2.7.3/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/bioinfp/jang/hobbyproggorn/gevent-messagepipe/gevent-messagepipe/pytest_mp_test.py", line 31, in child_unexpected_fail
    raise TypeError
TypeError
=========================== 1 failed, 2 passed in 0.07 seconds ===========================

答案 1 :(得分:3)

import pytest

@pytest.fixture
def runproc(request):
    import multiprocessing
    def Process(target):
        p = multiprocessing.Process(target=target)
        p.start()
        p.join()
        return p
    return Process

class CustomError(Exception):
    pass


def test_mp_expected_fail(runproc):
    p = runproc(child_expected_fail)
    assert not p.exitcode

def test_mp_success(runproc):
    p = runproc(target=child)
    assert not p.exitcode

def test_mp_unexpected_fail(runproc):
    p = runproc(child_unexpected_fail)
    assert not p.exitcode

def child_expected_fail():
    with pytest.raises(CustomError):
        raise CustomError

def child_unexpected_fail():
    raise TypeError

def child():
    pass

如果将此“runproc”夹具放入conftest.py文件中,则可以在项目的任何函数中接受“runproc”。