Back to Prefect

cli

docs/v3/api-ref/python/prefect-testing-cli.mdx

3.6.30.dev34.2 KB
Original Source

prefect.testing.cli

Functions

check_contains <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L183" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
check_contains(cli_result: CycloptsResult, 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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L232" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
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) -> CycloptsResult

Test utility for the Prefect CLI application.

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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L369" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
temporary_console_width(console: Console, width: int)

Classes

CycloptsResult <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L43" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L66" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/testing/cli.py#L87" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
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.