我正在使用 py.test 来运行测试。我将它与 pytest-xdist 一起使用来并行运行测试。我想在测试中查看打印语句的输出。
我有: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 and test_b, 正如它应该。
当我跑步时py.test-s-n2,再次没有打印任何内容。如何在使用时使打印语句正常工作-n2?
我已经读过pytest + xdist 不捕获输出 https://stackoverflow.com/questions/27006884/pytest-xdist-without-capturing-output和这个错误报告 https://github.com/pytest-dev/pytest/issues/680.
我刚刚在github上看到了关于这个问题的解释https://github.com/pytest-dev/pytest/issues/1693 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'
这不是一个好主意,但是工作。
您还应该注意 arg-s
如果您使用 xdist 插件则没有用。
In 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')
要查看日志文件,请创建一个名为的文件pytest.ini
并将以下内容添加到其中
log_cli = 1
log_cli_level = WARN
log_file = ./TestResults/test_log.log
log_file_level = WARN
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)