Struct assert_cmd::assert::Assert
source · [−]pub struct Assert { /* private fields */ }
Expand description
Assert the state of an Output
.
Create an Assert
through the OutputAssertExt
trait.
Examples
use assert_cmd::prelude::*;
use std::process::Command;
let mut cmd = Command::cargo_bin("bin_fixture")
.unwrap();
cmd.assert()
.success();
Implementations
sourceimpl Assert
impl Assert
sourcepub fn append_context<D>(self, name: &'static str, context: D) -> Selfwhere
D: Display + Send + Sync + 'static,
pub fn append_context<D>(self, name: &'static str, context: D) -> Selfwhere
D: Display + Send + Sync + 'static,
Clarify failures with additional context.
Examples
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.assert()
.append_context("main", "no args")
.success();
sourcepub fn get_output(&self) -> &Output
pub fn get_output(&self) -> &Output
Access the contained Output
.
sourcepub fn success(self) -> Self
pub fn success(self) -> Self
Ensure the command succeeded.
Examples
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.assert()
.success();
sourcepub fn try_success(self) -> AssertResult
pub fn try_success(self) -> AssertResult
try_
variant of Assert::success
.
sourcepub fn failure(self) -> Self
pub fn failure(self) -> Self
Ensure the command failed.
Examples
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "1")
.assert()
.failure();
sourcepub fn try_failure(self) -> AssertResult
pub fn try_failure(self) -> AssertResult
Variant of Assert::failure
that returns an AssertResult
.
sourcepub fn interrupted(self) -> Self
pub fn interrupted(self) -> Self
Ensure the command aborted before returning a code.
sourcepub fn try_interrupted(self) -> AssertResult
pub fn try_interrupted(self) -> AssertResult
Variant of Assert::interrupted
that returns an AssertResult
.
sourcepub fn code<I, P>(self, pred: I) -> Selfwhere
I: IntoCodePredicate<P>,
P: Predicate<i32>,
pub fn code<I, P>(self, pred: I) -> Selfwhere
I: IntoCodePredicate<P>,
P: Predicate<i32>,
Ensure the command returned the expected code.
This uses IntoCodePredicate
to provide short-hands for common cases.
See predicates
for more predicates.
Examples
Accepting a predicate:
use assert_cmd::prelude::*;
use std::process::Command;
use predicates::prelude::*;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(predicate::eq(42));
Accepting an exit code:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(42);
Accepting multiple exit codes:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(&[2, 42] as &[i32]);
sourcepub fn try_code<I, P>(self, pred: I) -> AssertResultwhere
I: IntoCodePredicate<P>,
P: Predicate<i32>,
pub fn try_code<I, P>(self, pred: I) -> AssertResultwhere
I: IntoCodePredicate<P>,
P: Predicate<i32>,
Variant of Assert::code
that returns an AssertResult
.
sourcepub fn stdout<I, P>(self, pred: I) -> Selfwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
pub fn stdout<I, P>(self, pred: I) -> Selfwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
Ensure the command wrote the expected data to stdout
.
This uses IntoOutputPredicate
to provide short-hands for common cases.
See predicates
for more predicates.
Examples
Accepting a bytes predicate:
use assert_cmd::prelude::*;
use std::process::Command;
use predicates::prelude::*;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::eq(b"hello\n" as &[u8]));
Accepting a str
predicate:
use assert_cmd::prelude::*;
use std::process::Command;
use predicates::prelude::*;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::str::diff("hello\n"));
Accepting bytes:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(b"hello\n" as &[u8]);
Accepting a str
:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout("hello\n");
sourcepub fn try_stdout<I, P>(self, pred: I) -> AssertResultwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
pub fn try_stdout<I, P>(self, pred: I) -> AssertResultwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
Variant of Assert::stdout
that returns an AssertResult
.
sourcepub fn stderr<I, P>(self, pred: I) -> Selfwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
pub fn stderr<I, P>(self, pred: I) -> Selfwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
Ensure the command wrote the expected data to stderr
.
This uses IntoOutputPredicate
to provide short-hands for common cases.
See predicates
for more predicates.
Examples
Accepting a bytes predicate:
use assert_cmd::prelude::*;
use std::process::Command;
use predicates::prelude::*;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::eq(b"world\n" as &[u8]));
Accepting a str
predicate:
use assert_cmd::prelude::*;
use std::process::Command;
use predicates::prelude::*;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::str::diff("world\n"));
Accepting bytes:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(b"world\n" as &[u8]);
Accepting a str
:
use assert_cmd::prelude::*;
use std::process::Command;
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr("world\n");
sourcepub fn try_stderr<I, P>(self, pred: I) -> AssertResultwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
pub fn try_stderr<I, P>(self, pred: I) -> AssertResultwhere
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
Variant of Assert::stderr
that returns an AssertResult
.