使用pytest和xdist时如何打印输出

时间:2016-04-19 18:21:06

标签: pytest xdist

我使用py.test来运行测试。我和pytest-xdist一起使用它来并行运行测试。我希望在我的测试中看到print语句的输出。

我有:Ubuntu 15.10,Python 2.7.10,pytest-2.9.1,pluggy-0.3.1。

这是我的测试文件:

def test_a():
    print 'test_a'


def test_b():
    print 'test_b'

当我运行 py.test 时,没有打印任何内容。这是预期的:默认情况下,py.test捕获输出。

当我运行 py.test -s 时,会按原样输出 test_a test_b

当我运行 py.test -s -n2 时,再没有打印任何内容。如何在使用 -n2

时使打印语句正常工作

我已经阅读了pytest + xdist without capturing output和此bug report

1 个答案:

答案 0 :(得分:3)

我只是在github https://github.com/pytest-dev/pytest/issues/1693

中看到有关此问题的解释

pytest-xdist使用sys stdin / stdout来控制执行, 显示打印输出的唯一方法应该是std.err。

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


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

不是一个好主意,而是工作。

另外你应该注意,如果使用xdist插件,arg -s没有用。

python 3 中,我认为logging.warning是更好的选择,因为它默认设置为写入stderr。

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

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


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