我在一个大量使用多处理的模块的上下文中在 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 的孩子中自动检测异常。
我必须在父母中验证孩子的退出代码吗?这是唯一的方法吗?