Attribute Macro rstest_reuse::apply

source ·
#[apply]
Expand description

Apply a defined template. The function signature should satisfy the template attributes but can also add some other fixtures. Example:

use rstest::{rstest, fixture};
use rstest_reuse::{self, *};

#[fixture]
fn empty () -> Vec<u32> {
    Vec::new()    
}

#[template]
#[rstest]
#[case(2, 2)]
#[case(4/2, 2)]
fn two_simple_cases(#[case] a: u32, #[case] b: u32) {}

#[apply(two_simple_cases)]
fn it_works(mut empty: Vec<u32>, a: u32, b: u32) {
    empty.append(a);
    assert!(empty.last() == b);
}

When use #[apply] you can also

  1. Use a path for template
  2. Ignore an argument by underscore
  3. add some cases
  4. add some values
use rstest::{rstest, fixture};
use rstest_reuse::{self, *};

#[fixture]
fn fix (#[default(0)] inner: u32) -> u32 {
    inner
}

mod outer {
    pub(crate) mod inner {
        use rstest_reuse::template;

        #[template]
        #[rstest]
        #[case(2, 2)]
        #[case(4/2, 2)]
        fn two_simple_cases(#[case] a: u32, #[case] b: u32) {}
    }
}


#[apply(outer::inner::two_simple_cases)]
// Add a case
#[case(9/3, 3)]
// Use fixture with 42 as argument
// Ignore b case values
// add 2 cases with other in 4, 5 for each case
fn lot_of_tests(fix: u32, a: u32, _b: u32, #[values(4, 5)] other: u32) {
    assert_eq!(fix, 42);
    assert_eq!(a, 2);
    assert!([4, 5].contains(other));
}