Crate assert_cmd

source ·
Expand description

Assert Command - Easy command initialization and assertions.

assert_cmd aims to simplify the process for doing integration testing of CLIs, including:

  • Finding your crate’s binary to test
  • Assert on the result of your program’s run.

Overview

Create a Command:

  • Command::new(path)
  • Command::from_std(...)
  • Command::cargo_bin(name)

Configure a Command:

  • arg / args
  • current_dir
  • env / envs / env_remove / env_clear
  • write_stdin / pipe_stdin
  • timeout

Validate a Command:

Note: Command is provided as a convenience. Extension traits for std::process::Command and Output are provided for interoperability:

Examples

Here’s a trivial example:

use assert_cmd::Command;

let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.assert().success();

And a little of everything:

use assert_cmd::Command;

let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let assert = cmd
    .arg("-A")
    .env("stdout", "hello")
    .env("exit", "42")
    .write_stdin("42")
    .assert();
assert
    .failure()
    .code(42)
    .stdout("hello\n");

Relevant crates

Other crates that might be useful in testing command line programs.

  • escargot for more control over configuring the crate’s binary.
  • duct for orchestrating multiple processes.
  • rexpect for testing interactive programs.
  • assert_fs for filesystem fixtures and assertions.
    • or tempfile for scratchpad directories.
  • dir-diff for testing file side-effects.

Migrating from assert_cli v0.6

assert_cmd is the successor to the original assert_cli:

  • More flexible, reusable assertions (also used by assert_fs).
  • Can integrate with other process-management crates, like duct.
  • Addresses several architectural problems.

Key points in migrating from assert_cli:

  • The command-under-test is run eagerly, with assertions happening immediately.
  • success() is not implicit and requires being explicitly called.
  • stdout/stderr aren’t automatically trimmed before being passed to the Predicate.

Re-exports

pub use crate::cmd::Command;

Modules

Simplify running bins in a Cargo project.
std::process::Command customized for testing.
Simplify one-off runs of programs.
Extension traits that are useful to have available.

Macros

Allows you to pull the name from your Cargo.toml at compile time.