How to let pytest hooks print to the console?

2020-04-29 01:16发布

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.

标签: python pytest
1条回答
家丑人穷心不美
2楼-- · 2020-04-29 01:35

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")
查看更多
登录 后发表回答