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§

source§

impl Assert

source

pub fn new(output: Output) -> Self

Create an Assert for a given Output.

source

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();
source

pub fn get_output(&self) -> &Output

Access the contained Output.

source

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();
source

pub fn try_success(self) -> AssertResult

try_ variant of Assert::success.

source

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();
source

pub fn try_failure(self) -> AssertResult

Variant of Assert::failure that returns an AssertResult.

source

pub fn interrupted(self) -> Self

Ensure the command aborted before returning a code.

source

pub fn try_interrupted(self) -> AssertResult

Variant of Assert::interrupted that returns an AssertResult.

source

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]);
source

pub fn try_code<I, P>(self, pred: I) -> AssertResultwhere I: IntoCodePredicate<P>, P: Predicate<i32>,

Variant of Assert::code that returns an AssertResult.

source

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");
source

pub fn try_stdout<I, P>(self, pred: I) -> AssertResultwhere I: IntoOutputPredicate<P>, P: Predicate<[u8]>,

Variant of Assert::stdout that returns an AssertResult.

source

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");
source

pub fn try_stderr<I, P>(self, pred: I) -> AssertResultwhere I: IntoOutputPredicate<P>, P: Predicate<[u8]>,

Variant of Assert::stderr that returns an AssertResult.

Trait Implementations§

source§

impl Debug for Assert

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Assert

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Assert

§

impl Send for Assert

§

impl Sync for Assert

§

impl Unpin for Assert

§

impl !UnwindSafe for Assert

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.