Skip to content

Configuring assertions

All assertions have some optional keyword arguments that you can pass in to configure their behavior.

Negation

Add in the negate = true argument to make the assertion have the opposite of its normal behavior. For example:

assert_str_contains!("hello, world", "asdf", negate = true);

This ensures that "hello, world" does not contain "asdf".

Descriptions

You can add a description to an assertion by passing in the description = <message> argument. For example:

let x = 1.0;
let y = 1.05;

assert_le!(
    (x - y).abs(),
    0.1,
    description = "x should be within 0.1 of y"
);

You can also add formatting with the description_owned = <message> argument:

const THRESHOLD: f32 = 0.1;

let x = 1.0;
let y = 1.05;

assert_le!(
    (x - y).abs(),
    THRESHOLD,
    description_owned = format!("x should be within {} of y", THRESHOLD)
);

description accepts as &str value while description_owned accepts a String value.