如何让 pytest 钩子打印到控制台? [英] How to let pytest hooks print to the console?

查看:50
本文介绍了如何让 pytest 钩子打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 pytest -s.但是,我想让钩子打印到控制台.

I know about pytest -s. However, I would like to let a hook print to the console.

以下对我不起作用:

terminal_reporter = request.config.pluginmanager.getplugin("terminalreporter")
terminal_reporter.write_line("TEST")

这仍然需要 pytest -s 才能运行.相反,我想规避这一点.

This still needed pytest -s to run. Instead I'd like to circumvent that.

具体来说,我正在覆盖 pytest_bdd_before_scenario() 钩子以打印正在执行的步骤.

Specifically, I'm overwriting the pytest_bdd_before_scenario() hook to print the steps that are being executed.

推荐答案

对于不参与测试执行(配置、报告等)的钩子,使用终端报告器编写应该可以工作.但是,一旦测试开始(并启用捕获),就会调用输出捕获机制,终端报告器也不例外.为了能够写入终端,您需要暂时禁用捕获.示例:

For the hooks that are not involved in test execution (configuration, reporting etc) writing with terminal reporter should work. However, once the test starts (and capturing is enabled), the output capturing mechanism is invoked, and there's no exception made for the terminal reporter. To be able to write to terminal, you need to disable capturing temporarily. Example:

terminal_reporter = request.config.pluginmanager.get_plugin('terminalreporter')
capture_manager = request.config.pluginmanager.get_plugin('capturemanager')
with capture_manager.global_and_fixture_disabled():
    terminal_reporter.write("TEST")

这篇关于如何让 pytest 钩子打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆