macro_rules! prop_assert {
    ($cond:expr) => { ... };
    ($cond:expr, $($fmt:tt)*) => { ... };
}
Expand description

Similar to assert! from std, but returns a test failure instead of panicking if the condition fails.

This can be used in any function that returns a Result<_, TestCaseError>, including the top-level function inside proptest!.

Both panicking via assert! and returning a test case failure have the same effect as far as proptest is concerned; however, the Rust runtime implicitly prints every panic to stderr by default (including a backtrace if enabled), which can make test failures unnecessarily noisy. By using prop_assert! instead, the only output on a failing test case is the final panic including the minimal test case.

Example

use proptest::prelude::*;

proptest! {
  #[test]
  fn triangle_inequality(a in 0.0f64..10.0, b in 0.0f64..10.0) {
    // Called with just a condition will print the condition on failure
    prop_assert!((a*a + b*b).sqrt() <= a + b);
    // You can also provide a custom failure message
    prop_assert!((a*a + b*b).sqrt() <= a + b,
                 "Triangle inequality didn't hold for ({}, {})", a, b);
    // If calling another function that can return failure, don't forget
    // the `?` to propagate the failure.
    assert_from_other_function(a, b)?;
  }
}

// The macro can be used from another function provided it has a compatible
// return type.
fn assert_from_other_function(a: f64, b: f64) -> Result<(), TestCaseError> {
  prop_assert!((a*a + b*b).sqrt() <= a + b);
  Ok(())
}