You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enable CliRunner to echo output to stdout/stderr #737
I'm driving my CliRunner testing from pytest. pytest will capture stdout/stderr by default, and display them when an assertion fails. As CliRunner captures all of the output itself, I don't get any guidance as to what my application has done on failure, without adding explicit debugging code to each assertion.
I'd like to be able to do something like CliRunner(echo=True) so that I get the output of its run both in its attributes and actually written to stdout/stderr.
Reacted by Mike Ross, Richard Nguyen, Mike Nerone, Mohinish, Dušan Juretić, Nathan Broadbent, Adam Uhlíř, Julien Nicoulaud, Petr Šplíchal, Andrew Henry and 51 more
I haven't dug deep into the internals for CliRunner.isolation() here, so I'm probably not doing this in the most correct way, but here's my workaround for anyone else interested:
import functools
import pytest
@pytest.fixture
def cli():
"""Yield a click.testing.CliRunner to invoke the CLI."""
class_ = click.testing.CliRunner
def invoke_wrapper(f):
"""Augment CliRunner.invoke to emit its output to stdout.
This enables pytest to show the output in its logs on test
failures.
"""
@functools.wraps(f)
def wrapper(*args, **kwargs):
echo = kwargs.pop('echo', False)
result = f(*args, **kwargs)
if echo is True:
sys.stdout.write(result.output)
return result
return wrapper
class_.invoke = invoke_wrapper(class_.invoke)
cli_runner = class_()
yield cli_runner
...
def test_basic(cli, package):
result = cli.invoke(package, ['--help])
assert result.exit_code == 0
Reacted by Jim Kelly, rndrr, Andrew Henry, Christian Sauer, aliaksei-siarhei, Andrii Oriekhov, Klemen Lovenjak, Maxim Stolyarchuk and Seung Hyun Kim
Reacted by Dušan Juretić, rndrr, Andrew Henry, Sylvain Marié, Thomas Marwitz and Seung Hyun Kim
Reacted by Klemen Lovenjak and Constantino Schillebeeckx
I also came up against this. For the record my solution was the following helper method:
def invoke(cmd, expect_exit=0, catch_exceptions=False, **kwargs):
runner = click.testing.CliRunner()
result = runner.invoke(cli, cmd, catch_exceptions=catch_exceptions, **kwargs)
if expect_exit is not None:
assert result.exit_code == expect_exit
return result
Activity
nguyening commentedon Jun 17, 2017
Would also love to see this.
I haven't dug deep into the internals for
CliRunner.isolation()here, so I'm probably not doing this in the most correct way, but here's my workaround for anyone else interested:mbirtwell commentedon Sep 15, 2022
I also came up against this. For the record my solution was the following helper method:
kdeldycke commentedon May 16, 2026
PR has been pushed to
mainand will be part of the imminent 8.4.0 release.