将pytest与xdist一起使用时如何打印输出 [英] How to print output when using pytest with xdist
问题描述
我正在使用py.test来运行测试.我将其与pytest-xdist一起使用以并行运行测试.我想在测试中查看打印语句的输出.
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.
我有:Ubuntu 15.10,Python 2.7.10,pytest-2.9.1,pluggy-0.3.1.
I have: 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会捕获输出.
When I run py.test, nothing is printed. That's expected: by default, py.test captures output.
当我运行 py.test -s 时,它会打印 test_a 和 test_b .
When I run py.test -s, it prints test_a and test_b, as it should.
当我运行 py.test -s -n2 时,再次没有打印任何内容.在使用 -n2 时如何使打印语句正常工作?
When I run py.test -s -n2, again nothing is printed. How can I get the print statements to work while using -n2?
我已经阅读了 pytest + xdist而没有捕获到输出,并且此错误报告.
I've already read pytest + xdist without capturing output and this bug report.
推荐答案
我只在github https://github.com/pytest-dev/pytest/issues/1693
I just see the explain about this issue in github https://github.com/pytest-dev/pytest/issues/1693
pytest-xdist
使用sys stdin/stdout控制执行,
唯一显示打印输出的方法应该是std.err.
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.
还应注意,如果使用xdist插件,则arg -s
没有任何作用.
Also you should note that the arg -s
has no use if you use xdist plugin.
在 python 3 中,我认为logging.warning
是更好的选择,因为默认情况下已将其设置为写入stderr.
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')
这篇关于将pytest与xdist一起使用时如何打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!