Skip to main content

CliRunner

This class provides a specialized runner for testing command-line applications by wrapping the standard Click CLI runner. It simplifies the process of invoking commands by automatically extracting the underlying command structure from a Typer application instance. This allows developers to programmatically simulate CLI execution, capture output, and verify application behavior in a controlled environment.

Methods


invoke()

@classmethod
def invoke(
app: [Typer](../main/typer.md?sid=typer_main_typer),
args: str | Sequence[str]| None = None,
input: bytes | str | IO[Any]| None = None,
env: Mapping[str, str | None]| None = None,
catch_exceptions: bool = True,
color: bool = False,
**extra: Any
) - > Result

Invokes a Typer application as a command-line script for testing purposes. This method wraps the underlying Click runner to handle Typer-specific command resolution before execution.

Parameters

NameTypeDescription
app[Typer](../main/typer.md?sid=typer_main_typer)The Typer application instance to be executed.
args`strSequence[str]
input`bytesstr
env`Mapping[str, strNone]
catch_exceptionsbool = TrueWhether to catch exceptions and include them in the Result object instead of letting them propagate.
colorbool = FalseWhether to enable ANSI color codes in the output.
**extraAnyAdditional keyword arguments passed directly to the underlying Click CliRunner.invoke method.

Returns

TypeDescription
ResultA Result object containing the captured stdout/stderr, exit code, and any raised exceptions.