prefect.testing.cli
Functions
check_contains
should_contain: if True, checks that content is in cli_result, if False, checks that content is not in cli_result
invoke_and_assert
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
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
fileproperty) 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.
invoke
args: Command-line arguments (e.g. [“config”, “view”]).input: Simulated stdin content for interactive prompts.
- CycloptsResult with captured stdout, stderr, exit_code, and
- any exception that occurred.