python如何打印输出,python - 如何在pytest运行期间看到正常的打印输出?

在接受回答的评论中,乔问道:

有没有办法打印到控制台并捕获输出,以便它显示在junit报告中?

在UNIX中,这通常被称为teeing。 理想情况下,teeing而不是捕获将是py.test默认值。 非理想情况下,py.test和任何现有的第三方py.test插件(我知道,无论如何)都不支持发球 - 尽管Python平凡地支持开箱即用的发球。

猴子修补py.test做任何不受支持的事情都是非平凡的。 为什么? 因为:

大多数py.test功能都锁定在私有PyTest.run_tests()软件包后面,不打算从外部导入。 试图在不知道你正在做什么的情况下这样做通常会导致公共--capture=no包在运行时引发模糊的异常。 非常感谢,py.test。 你到那里真的很健壮的建筑。

即使您确定如何以安全的方式修补私有PyTest.run_tests() API,也必须在运行外部py.test命令运行的公共--capture=no程序包之前执行此操作。 您无法在插件中执行此操作(例如,测试套件中的顶级conftest模块)。 当py.test懒惰地动态导入你的插件时,你想要实现猴子补丁的任何py.test类早已被实例化 - 并且你无权访问该实例。 这意味着,如果您希望有意义地应用猴子补丁,则无法再安全地运行外部py.test命令。 相反,您必须使用自定义setuptools test命令(按顺序)包装该命令的运行:Monkey-patches私有PyTest.run_tests() API。

调用public PyTest.run_tests()函数来运行--capture=no命令。

这个答案猴子补丁py.test的PyTest.run_tests()和--capture=no选项捕获stderr但不是stdout。 默认情况下,这些选项既不捕获stderr也不捕获stdout。 当然,这不是很发球。 但是每一个伟大的旅程都始于一个繁琐的前传,每个人都会忘记五年。

为什么这样? 我现在要告诉你。 我的py.test驱动的测试套件包含慢速功能测试。 显示这些测试的标准输出是有帮助和安慰的,当另一个长期运行的功能测试在几周内没有做任何事情时,防止leycec到达PyTest.run_tests()。 但是,显示这些测试的stderr会阻止py.test报告测试失败时的异常回溯。 这完全没有用。 因此,我们强制py.test捕获stderr而不是stdout。

在我们开始之前,这个答案假设您已经有一个自定义setuptools PyTest.run_tests()命令调用py.test。 如果不这样做,请参阅py.test编写良好的良好实践页面的手册集成小节。

不要安装pytest-runner,第三方setuptools插件,提供自定义setuptools PyTest.run_tests()命令,同时调用py.test。 如果已安装pytest-runner,您可能需要卸载该pip3包,然后采用上面链接的手动方法。

假设您按照上面突出显示的手动积分中的说明进行操作,您的代码库现在应该包含PyTest.run_tests()方法。 修改此方法以类似于:

class PyTest(TestCommand):

.

.

.

def run_tests(self):

# Import the public "pytest" package *BEFORE* the private "_pytest"

# package. While importation order is typically ignorable, imports can

# technically have side effects. Tragicomically, that is the case here.

# Importing the public "pytest" package establishes runtime

# configuration required by submodules of the private "_pytest" package.

# The former *MUST* always be imported before the latter. Failing to do

# so raises obtuse exceptions at runtime... which is bad.

import pytest

from _pytest.capture import CaptureManager, FDCapture, MultiCapture

# If the private method to be monkey-patched no longer exists, py.test

# is either broken or unsupported. In either case, raise an exception.

if not hasattr(CaptureManager, '_getcapture'):

from distutils.errors import DistutilsClassError

raise DistutilsClassError(

'Class "pytest.capture.CaptureManager" method _getcapture() '

'not found. The current version of py.test is either '

'broken (unlikely) or unsupported (likely).'

)

# Old method to be monkey-patched.

_getcapture_old = CaptureManager._getcapture

# New method applying this monkey-patch. Note the use of:

#

# * "out=False", *NOT* capturing stdout.

# * "err=True", capturing stderr.

def _getcapture_new(self, method):

if method == "no":

return MultiCapture(

out=False, err=True, in_=False, Capture=FDCapture)

else:

return _getcapture_old(self, method)

# Replace the old with the new method.

CaptureManager._getcapture = _getcapture_new

# Run py.test with all passed arguments.

errno = pytest.main(self.pytest_args)

sys.exit(errno)

要启用此monkey-patch,请运行py.test,如下所示:

python setup.py test -a "-s"

现在将捕获Stderr而不是stdout。漂亮!

将上面的猴子补丁扩展到tee stdout和stderr,留给玩家一个充满空闲时间的练习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值