How to let pytest hooks print to the console?

一个人想着一个人 提交于 2019-12-13 19:51:03

问题


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

The following didn't work for me:

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

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

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


回答1:


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")


来源:https://stackoverflow.com/questions/57201523/how-to-let-pytest-hooks-print-to-the-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!