macro_rules! proptest {
(#![proptest_config($config:expr)]
$(
$(#[$meta:meta])*
fn $test_name:ident($($parm:pat in $strategy:expr),+) $body:block
)*) => { ... };
($(
$(#[$meta:meta])*
fn $test_name:ident($($parm:pat in $strategy:expr),+) $body:block
)*) => { ... };
}
Expand description
Easily define proptest
tests.
Within proptest!
, define one or more functions without return type
normally, except instead of putting : type
after each parameter, write
in strategy
, where strategy
is an expression evaluating to some
Strategy
.
Each function will be wrapped in a function which sets up a TestRunner
,
and then invokes the function body with inputs generated according to the
strategies. Note that the inputs are borrowed from the test runner, so if
they are not Copy
, you will need to use ref
with each parameter name.
Example:
#[macro_use] extern crate proptest;
proptest! {
#[test]
fn test_addition(a in 0..10, b in 0..10) {
prop_assert!(a + b <= 18);
}
// Note the `ref a` and `ref b` --- `String` is not `Copy`,
// so we can't take ownership implicitly.
#[test]
fn test_string_concat(ref a in ".*", ref b in ".*") {
let cat = format!("{}{}", a, b);
prop_assert_eq!(a.len() + b.len(), cat.len());
}
}
To override the default configuration, you can start the proptest!
block
with #![proptest_config(expr)]
, where expr
is an expression that
evaluates to a proptest::test_runner::Config
(or a reference to one).
#[macro_use] extern crate proptest;
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig {
cases: 99, .. ProptestConfig::default()
})]
#[test]
fn test_addition(a in 0..10, b in 0..10) {
prop_assert!(a + b <= 18);
}
}