[−][src]Attribute Macro rstest::fixture
#[fixture]
Define a fixture that you can use in all rstest
's test arguments. You should just mark your
function as [fixture]
and then use it as a test's argument. Fixture functions can also
use other fixtures.
Let's see a trivial example:
use rstest::*; #[fixture] fn twenty_one() -> i32 { 21 } #[fixture] fn two() -> i32 { 2 } #[fixture] fn injected(twenty_one: i32, two: i32) -> i32 { twenty_one * two } #[rstest] fn the_test(injected: i32) { assert_eq!(42, injected) }
You can also partialy inject fixture dependency simply indicate dependency value as fixture argument:
use rstest::*; #[fixture] fn base() -> i32 { 1 } #[fixture] fn first(base: i32) -> i32 { 1 * base } #[fixture] fn second(base: i32) -> i32 { 2 * base } #[fixture(second(-3))] fn injected(first: i32, second: i32) -> i32 { first * second } #[rstest] fn the_test(injected: i32) { assert_eq!(-6, injected) }
Note that injected value can be an arbitrary rust expression and not just a literal.
Sometimes the return type cannot be infered so you must define it: For the few times you may
need to do it, you can use the default<type>
, partial_n<type>
attribute syntax to define it:
use rstest::*; #[fixture] pub fn i() -> u32 { 42 } #[fixture] pub fn j() -> i32 { -42 } #[fixture(::default<impl Iterator<Item=(u32, i32)>>::partial_1<impl Iterator<Item=(I,i32)>>)] pub fn fx<I, J>(i: I, j: J) -> impl Iterator<Item=(I, J)> { std::iter::once((i, j)) } #[rstest] fn resolve_by_default<I: Debug + PartialEq>(mut fx: impl Iterator<Item=I>) { assert_eq!((42, -42), fx.next().unwrap()) } #[rstest(fx(42.0))] fn resolve_partial<I: Debug + PartialEq>(mut fx: impl Iterator<Item=I>) { assert_eq!((42.0, -42), fx.next().unwrap()) }
partial_i
is the fixture used when you inject the first i
arguments in test call.