Skip to main content

prefect.testing.cli

Functions

check_contains

check_contains(cli_result: Result, content: str, should_contain: bool) -> None
Utility function to see if content is or is not in a CLI result. Args:
  • should_contain: if True, checks that content is in cli_result, if False, checks that content is not in cli_result

invoke_and_assert

invoke_and_assert(command: str | list[str], user_input: str | None = None, prompts_and_responses: list[tuple[str, str] | tuple[str, str, str]] | None = None, expected_output: str | None = None, expected_output_contains: str | Iterable[str] | None = None, expected_output_does_not_contain: str | Iterable[str] | None = None, expected_line_count: int | None = None, expected_code: int | None = 0, echo: bool = True, temp_dir: str | None = None) -> Result | CycloptsResult
Test utility for the Prefect CLI application. Supports both the typer CLI (default) and the cyclopts CLI (when PREFECT_CLI_FAST=1 is set). The cyclopts path uses CycloptsCliRunner for in-process invocation with proper I/O isolation. Args:
  • command: Command-line arguments (string or list of strings).
  • user_input: Simulated stdin for interactive commands.
  • prompts_and_responses: List of (prompt, response[, selected_option]) tuples for interactive commands.
  • expected_output: Assert exact match with CLI output.
  • expected_output_contains: Assert CLI output contains this string or each string in the iterable.
  • expected_output_does_not_contain: Assert CLI output does not contain this string or any string in the iterable.
  • expected_line_count: Assert the number of output lines.
  • expected_code: Expected exit code (default 0).
  • echo: Print CLI output for debugging (default True).
  • temp_dir: Run the command in this directory.

temporary_console_width

temporary_console_width(console: Console, width: int)

Classes

CycloptsResult

Result of a cyclopts CLI invocation. Compatible with typer’s Result so existing invoke_and_assert callers can work with either runner without changes.

CycloptsCliRunner

In-process test runner for the cyclopts CLI. Analogous to Click’s CliRunner: captures stdout/stderr, simulates stdin, emulates a TTY for Rich Console interactive mode, and isolates global state between invocations. Design principles:
  • Use a TTY-emulating StringIO as sys.stdout so that Rich Console instances (which resolve sys.stdout dynamically via their file property) write to our capture buffer AND report is_interactive=True.
  • Redirect sys.stdin to a StringIO for prompt input.
  • Save and restore all mutated global state (sys.stdout/stderr/stdin, os.environ[“COLUMNS”], the cyclopts app’s global console) in a try/finally block.
  • Catch SystemExit to extract exit codes without terminating the process.
Not thread-safe (mutates interpreter globals), but safe with pytest-xdist which forks separate worker processes. Methods:

invoke

invoke(self, args: str | list[str], input: str | None = None) -> CycloptsResult
Invoke the cyclopts CLI with the given arguments. Args:
  • args: Command-line arguments (e.g. [“config”, “view”]).
  • input: Simulated stdin content for interactive prompts.
Returns:
  • CycloptsResult with captured stdout, stderr, exit_code, and
  • any exception that occurred.