[][src]Attribute Macro rstest::rstest_parametrize

#[rstest_parametrize]
Deprecated since 0.5.0:

Please use #[rstest(...)] instead

Write table-based tests: you must indicate the arguments that you want use in your cases and provide them for each case you want to test.

rstest_parametrize generates an independent test for each case.

#[rstest_parametrize(input, expected,
    case(0, 0),
    case(1, 1),
    case(2, 1),
    case(3, 2),
    case(4, 3)
)]
fn fibonacci_test(input: u32, expected: u32) {
    assert_eq!(expected, fibonacci(input))
}

Running cargo test in this case executes five tests:

running 5 tests
test fibonacci_test::case_1 ... ok
test fibonacci_test::case_2 ... ok
test fibonacci_test::case_3 ... ok
test fibonacci_test::case_4 ... ok
test fibonacci_test::case_5 ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Like in [rstest] you can inject fixture values and every parameter that isn't mapped in case()s will be resolved as default fixture.

In general rstest_parametrize's syntax is:

rstest_parametrize(ident_1,..., ident_n,
    case[::description_1](val_1_1, ..., val_n_1),
    ...,
    case[::description_m](val_1_m, ..., val_n_m)[,]
    [fixture_1(...]
    [...,]
    [fixture_k(...)]
    [::attribute_1[:: ... [::attribute_k]]]
)
  • ident_x should be a valid function argument name
  • val_x_y should be a valid rust expression that can be assigned to ident_x function argument
  • description_z when present should be a valid Rust identity
  • fixture_v(...) should be a valid function argument and a [fixture] fixture function
  • attributes now can be just trace or notrace(args..) (see [rstest]

Function's arguments can be present just once as identity or fixture.

Functions marked by rstest_parametrize can use generics, impl and dyn without any restriction.

#[rstest_parametrize(input, expected,
    case("foo", 3),
    case(String::from("bar"), 3),
)]
fn len<S: AsRef<str>>(input: S, expected: usize) {
    assert_eq!(expected, input.as_ref().len())
}

#[rstest_parametrize(input, expected,
    case("foo", 3),
    case(String::from("bar"), 3),
)]
fn len_by_impl(input: impl AsRef<str>, expected: usize) {
    assert_eq!(expected, input.as_ref().len())
}