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
| Name | Type | Description |
|---|---|---|
| app | [Typer](../main/typer.md?sid=typer_main_typer) | The Typer application instance to be executed. |
| args | `str | Sequence[str] |
| input | `bytes | str |
| env | `Mapping[str, str | None] |
| catch_exceptions | bool = True | Whether to catch exceptions and include them in the Result object instead of letting them propagate. |
| color | bool = False | Whether to enable ANSI color codes in the output. |
| **extra | Any | Additional keyword arguments passed directly to the underlying Click CliRunner.invoke method. |
Returns
| Type | Description |
|---|---|
Result | A Result object containing the captured stdout/stderr, exit code, and any raised exceptions. |