How to print output when using pytest with xdist

冷暖自知 提交于 2019-12-10 01:59:18

问题


I'm using py.test to run tests. I'm using it with pytest-xdist to run the tests in parallel. I want to see the output of print statements in my tests.

I have: Ubuntu 15.10, Python 2.7.10, pytest-2.9.1, pluggy-0.3.1.

Here's my test file:

def test_a():
    print 'test_a'


def test_b():
    print 'test_b'

When I run py.test, nothing is printed. That's expected: by default, py.test captures output.

When I run py.test -s, it prints test_a and test_b, as it should.

When I run py.test -s -n2, again nothing is printed. How can I get the print statements to work while using -n2?

I've already read pytest + xdist without capturing output and this bug report.


回答1:


I just see the explain about this issue in github https://github.com/pytest-dev/pytest/issues/1693

pytest-xdist use sys stdin/stdout to control execution, the only way to show print out should be std.err.

import sys
def test_a():
    print >> sys.stderr, 'test_a'


def test_b():
    print >> sys.stderr, 'test_b'

Not a good idea, but work.

Also you should note that the arg -s has no use if you use xdist plugin.


In python 3, I think logging.warning is a better choice, since that it is set up to write to stderr by default.

import logging
logging.basicConfig(format='%(message)s')

def test_a():
    logging.warning('test_a')


def test_b():
    logging.warning('test_b')


来源:https://stackoverflow.com/questions/36726461/how-to-print-output-when-using-pytest-with-xdist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!