The Rust testing framework provides macros for test assertions, such as:
-
assert!(condition)
: assertcondition
is true. -
assert_eq!(a, b)
: asserta
is equal tob
. -
assert_ne!(a, b)
: asserta
is not equal tob
.
Example:
let x = 1;
let y = 2;
assert!(x < y);
Example with an optional message:
let x = 1;
let y = 2;
assert!(x < y, "We want x to be less than y");
The Assertables crate provides more assert macros, such as:
-
assert_starts_with!(x, y)
: Doesx
start withy
? -
assert_contains!(array, element)
: Doesarray
containselement
? -
assert_is_match(regex, string)
: Doesregex
matchstring
?string.
Example:
let a = "hello world";
let b = "hello";
assert_starts_with!(&a, &b);