pub struct CliRunner { /* private fields */ }Expand description
A test runner for CLI applications.
CliRunner provides a controlled environment for testing CLI commands.
It captures stdout, stderr, and tracks exit codes.
§Example
use click::testing::CliRunner;
use click::command::Command;
let cmd = Command::new("greet")
.callback(|ctx| {
let default_name = "World".to_string();
let name = ctx.get_param::<String>("name").unwrap_or(&default_name);
// Use name for greeting
Ok(())
})
.build();
let runner = CliRunner::new();
let result = runner.invoke(&cmd, &[]);
assert_eq!(result.exit_code, 0);Implementations§
Source§impl CliRunner
impl CliRunner
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new CLI runner with default settings.
§Example
use click::testing::CliRunner;
let runner = CliRunner::new();Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Set an environment variable for the invocation.
§Example
use click::testing::CliRunner;
let runner = CliRunner::new()
.env("MY_VAR", "value");Sourcepub fn env_unset(self, key: impl Into<String>) -> Self
pub fn env_unset(self, key: impl Into<String>) -> Self
Unset an environment variable for the invocation.
Sourcepub fn echo_stdin(self, echo: bool) -> Self
pub fn echo_stdin(self, echo: bool) -> Self
Set whether to echo stdin to output.
Sourcepub fn mix_stderr(self, mix: bool) -> Self
pub fn mix_stderr(self, mix: bool) -> Self
Set whether to mix stderr into stdout.
When true (default), InvokeResult.output contains stdout + stderr.
The InvokeResult.stderr field is always populated with captured stderr.
Sourcepub fn catch_panics(self, catch: bool) -> Self
pub fn catch_panics(self, catch: bool) -> Self
Set whether panics should be captured as a failure instead of re-panicking.
Sourcepub fn charset(self, charset: impl Into<String>) -> Self
pub fn charset(self, charset: impl Into<String>) -> Self
Set the charset used to decode captured output.
Defaults to "utf-8". Unknown labels fall back to UTF-8.
Sourcepub fn invoke(&self, cmd: &dyn CommandLike, args: &[&str]) -> InvokeResult
pub fn invoke(&self, cmd: &dyn CommandLike, args: &[&str]) -> InvokeResult
Invoke a command and capture the result.
§Arguments
cmd- The command to invokeargs- Command-line arguments
§Example
use click::testing::CliRunner;
use click::command::Command;
let cmd = Command::new("hello")
.callback(|_| {
println!("Hello!");
Ok(())
})
.build();
let result = CliRunner::new().invoke(&cmd, &[]);
assert_eq!(result.exit_code, 0);Sourcepub fn invoke_with_input(
&self,
cmd: &dyn CommandLike,
args: &[&str],
input: Option<&str>,
) -> InvokeResult
pub fn invoke_with_input( &self, cmd: &dyn CommandLike, args: &[&str], input: Option<&str>, ) -> InvokeResult
Invoke a command with simulated stdin input.
§Arguments
cmd- The command to invokeargs- Command-line argumentsinput- Optional stdin input
§Example
use click::testing::CliRunner;
use click::command::Command;
let cmd = Command::new("echo")
.callback(|_| Ok(()))
.build();
let result = CliRunner::new().invoke_with_input(&cmd, &[], Some("test input"));Sourcepub fn invoke_isolated(
&self,
cmd: &dyn CommandLike,
args: &[&str],
) -> InvokeResult
pub fn invoke_isolated( &self, cmd: &dyn CommandLike, args: &[&str], ) -> InvokeResult
Invoke a command within an isolated filesystem.
This creates a temporary directory and runs the command there.
§Arguments
cmd- The command to invokeargs- Command-line arguments
§Example
use click::testing::CliRunner;
use click::command::Command;
let cmd = Command::new("test").build();
let result = CliRunner::new().invoke_isolated(&cmd, &[]);