CLI testing¶
CliRunner runs a Click CLI in-process: the command object is imported, its callback executes inside the test process, and its streams are captured in memory. It is a drop-in replacement for click.testing.CliRunner, and what the runner and invoke fixtures hand to a test.
Tip
For the black-box counterpart, which spawns the command as a subprocess and never imports it, see test suites. Both render a run with the same execution trace.
Invoking a command¶
Take a CLI reporting the temperature of each city it is given:
from click_extra import argument, command, echo, option
@command
@option("--unit", default="celsius")
@argument("cities", nargs=-1)
def forecast(unit, cities):
"""Print the temperature of each city."""
if not cities:
echo("No city to report on.", err=True)
for city in cities:
echo(f"{city}: 21 {unit}")
Hand it to a runner. The command is the first positional argument, and everything after it is a command-line argument:
from click_extra.testing import CliRunner
runner = CliRunner()
result = runner.invoke(forecast, "--unit", "fahrenheit", "Oslo", "Lisbon")
assert result.exit_code == 0
assert result.stdout == "Oslo: 21 fahrenheit\nLisbon: 21 fahrenheit\n"
$ forecast --unit fahrenheit Oslo Lisbon
<output> stream:
Oslo: 21 fahrenheit
Lisbon: 21 fahrenheit
<stdout> stream:
Oslo: 21 fahrenheit
Lisbon: 21 fahrenheit
<exit_code>: 0
Passing the arguments as a list under an args keyword, the way vanilla Click expects them, works too and can be mixed with the positional form.
Every invocation prints the execution trace reproduced above, pass or fail. It is what a failing test shows in its captured output, so a broken assertion comes with the session that produced it instead of a bare exit code.
Composing arguments¶
Positional arguments are flattened, so a nested structure of lists and tuples is spelled out as-is. None values are dropped, and every remaining item is cast to a string, which lets a Path travel unconverted:
from pathlib import Path
result = runner.invoke(forecast, ["Oslo", None, ["Lisbon", (Path("Kyoto"),)]])
assert result.exit_code == 0
assert result.stdout == "Oslo: 21 celsius\nLisbon: 21 celsius\nKyoto: 21 celsius\n"
$ forecast Oslo Lisbon Kyoto
<output> stream:
Oslo: 21 celsius
Lisbon: 21 celsius
Kyoto: 21 celsius
<stdout> stream:
Oslo: 21 celsius
Lisbon: 21 celsius
Kyoto: 21 celsius
<exit_code>: 0
That is args_cleanup() doing the work, and it is what makes a parametrized test readable: each case contributes its own fragment of the command line, and an optional fragment collapses to None instead of forcing the test to assemble the list itself.
test_forecast.py¶import pytest
@pytest.mark.parametrize("unit_flag", (None, ("--unit", "fahrenheit")))
@pytest.mark.parametrize("cities", (("Oslo",), ("Oslo", "Lisbon")))
def test_forecast(invoke, unit_flag, cities):
result = invoke(forecast, unit_flag, cities)
assert result.exit_code == 0
Reading the result¶
Result carries the exit code, the captured streams and whatever exception escaped the callback:
Attribute |
Content |
|---|---|
|
Process exit status. |
|
Standard output alone. |
|
Standard error alone. |
|
Both streams, interleaved in the order they were written. |
|
The exception that escaped the callback, if any. |
|
Its full traceback, as a string, or |
The two streams stay addressable separately, which is how a test pins a diagnostic to the stream it belongs on:
result = runner.invoke(forecast)
assert result.exit_code == 0
assert result.stdout == ""
assert result.stderr == "No city to report on.\n"
$ forecast
<output> stream:
No city to report on.
<stderr> stream:
No city to report on.
<exit_code>: 0
When the callback raises, the runner catches the exception, sets a non-zero exit_code, and prints the traceback below the trace. formatted_exception keeps that traceback available, and repr(result) embeds it, so an assert result.exit_code == 0 that fails reports where the CLI died rather than which exception class it was. Pass catch_exceptions=False to let the exception propagate into the test instead, or set it once on the runner with CliRunner(catch_exceptions=False).
A command with a report_url handles an error it has no rule for itself. The run ends with a bug report on stderr and the exit code 1. The exception that raised never leaves the command, whatever catch_exceptions says.
Colors¶
ANSI codes survive the trip only when asked for. color keeps Click’s meaning, and force_color adds one more state:
Argument |
Captured streams |
|
|---|---|---|
|
Stripped |
|
|
Stripped |
|
|
Kept |
|
|
Kept |
|
color=True keeps the codes in the captured output, but the invoked CLI still sees an uncolored context and takes its uncolored branch. force_color=True covers both: it keeps the codes and initializes Context.color to True, which vanilla Click cannot express because the two meanings collide on one parameter name (pallets/click#2110). Click Extra routes the second one through a patched main() call, so any other Context keyword named like an invoke() parameter gets through as well.
import click
from click_extra import style
@click.command
@click.pass_context
def report(ctx):
click.echo(style("Sunny", fg="yellow") + " in Lisbon.")
click.echo(f"Context.color is {ctx.color!r}")
result = runner.invoke(report, force_color=True)
assert "\x1b[33mSunny\x1b[0m in Lisbon.\n" in result.stdout
assert "Context.color is True\n" in result.stdout
$ report
<output> stream:
Sunny in Lisbon.
Context.color is True
<stdout> stream:
Sunny in Lisbon.
Context.color is True
<exit_code>: 0
Setting color=False goes one step further than Click’s stripping and scrubs the result bytes, so a CLI writing raw escape sequences past Click’s own machinery still yields clean text. To force colors on every invocation of a suite without touching each call, set the force_color class attribute on CliRunner: it pins every run to the force_color=True row above, unless a call passes color=False.
Note
The command above is a plain Click one on purpose: nothing but the runner decides its colors. A Click Extra command owns that decision itself, through its --color/--no-color options and the NO_COLOR and FORCE_COLOR environment variables, which have the last word over whatever the runner was told.
Rendering a run¶
render_cli_run() produces the trace on its own, from either an in-process click.testing.Result or a subprocess.CompletedProcess. Both are normalized into a StreamView first, so a black-box run and an in-process one read identically:
import subprocess
import sys
from click_extra.testing import render_cli_run
process = subprocess.run(
(sys.executable, "-c", "print('Lisbon: 21 celsius')"),
capture_output=True,
text=True,
encoding="UTF-8",
)
print(render_cli_run(("forecast", "Lisbon"), process, env={"FORECAST_UNIT": "celsius"}))
$ FORECAST_UNIT=celsius forecast Lisbon
<stdout> stream:
Lisbon: 21 celsius
<exit_code>: 0
The first line is the command as the user would have typed it, environment assignments included. A variable mapped to None is removed from the environment, so the line opens on env -u and its name. Each captured stream then gets its own labelled, indented block, and the exit code closes the trace. A stream that captured nothing is left out entirely: a subprocess run with stderr=STDOUT reports a single interleaved <output> stream, while separate streams give the <stdout> and <stderr> pair.
Matching output against a regex¶
Comparing a wall of terminal output to an expected string reports the whole wall on failure, and leaves the reader to find the offending character. regex_fullmatch_line_by_line() matches the pattern one line at a time and reports the first line that disagrees:
from click_extra.testing import RegexLineMismatch, regex_fullmatch_line_by_line
try:
regex_fullmatch_line_by_line(
r"Lisbon: \d+ celsius\nOslo: \d+ fahrenheit\n",
"Lisbon: 21 celsius\nOslo: 9 celsius\n",
)
except RegexLineMismatch as ex:
print(ex)
Line #2 does not match.
Regex : 'Oslo: \\d+ fahrenheit\\n'
Output: 'Oslo: 9 celsius\n'
The pattern is split on its \n tokens, so it is written as one raw string mirroring the expected output, not as a list of per-line patterns. A pattern matching in full short-circuits the loop, and only a mismatch pays for the line-by-line pass.
The reported pattern goes through unescape_regex(), the inverse of re.escape(), which strips the backslashes an escaped literal is littered with so the two sides of the report line up visually:
from click_extra.testing import unescape_regex
assert unescape_regex(r"Usage: forecast \[OPTIONS\] \[CITIES\]\.\.\.") == (
"Usage: forecast [OPTIONS] [CITIES]..."
)
print(unescape_regex(r"Usage: forecast \[OPTIONS\] \[CITIES\]\.\.\."))
Usage: forecast [OPTIONS] [CITIES]...
The assert_output_regex fixture wraps that comparison into an assertion carrying a character-level diff, and the ready-made patterns shipped with it cover the help screen and debug logs Click Extra adds to every CLI.