Crate assert_cli [−] [src]
This crate's goal is to provide you some very easy tools to test your CLI applications. It can currently execute child processes and validate their exit status as well as stdout and stderr output against your assertions.
Include the crate like
#[macro_use] // <-- import the convenience macro (optional) extern crate assert_cli;
Here's a trivial example:
assert_cli::Assert::command(&["echo", "42"]) .stdout().contains("42") .unwrap();
And here is one that will fail:
assert_cli::Assert::command(&["echo", "42"]) .stdout().is("1337") .unwrap();
this will show a nice, colorful diff in your terminal, like this:
Alternatively, you can use the
assert_cmd! macro to construct the command more conveniently,
but please carefully read the limitations below, or this may seriously go wrong.
- Don't forget to import the crate with
- Enclose arguments in the
assert_cmd!macro in quotes
", if there are special characters, which the macro doesn't accept, e.g.
All assertion default to checking that the command exited with success.
However, when you expect a command to fail, you can express it like this:
assert_cmd!(cat "non-existing-file") .fails() .unwrap();
Some notes on this:
fails_withto assert a specific exit status.
- There is also a
succeedsmethod, but this is already the implicit default and can usually be omitted.
andmethod has no effect, other than to make everything more readable. Feel free to use it. :-)
You can add assertions on the content of stdout and stderr. They can be mixed together or even multiple of one stream can be used.
assert_cmd!(echo "Hello world! The ansswer is 42.") .stdout().contains("Hello world") .stdout().contains("42") .stderr().is("") .unwrap();
If you are testing a Rust binary crate, you can start with
Assert::main_binary() to use
cargo run as command. Or, if you want to
run a specific binary (if you have more than one), use
If you don't want it to panic when the assertions are not met, simply call
.execute instead of
.unwrap to get a
let x = assert_cmd!(echo "1337").stdout().is("42").execute(); assert!(x.is_err());
Easily construct an
Assertions for a specific command.
Structure to deal with environment variables
Assertions for command output.