How to print output when using pytest with xdist

2019-04-24 13:00发布

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.

标签: pytest xdist
1条回答
戒情不戒烟
2楼-- · 2019-04-24 13:14

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')
查看更多
登录 后发表回答