[][src]Attribute Macro devbox_test_args::args

#[args]

This is an attribute complementing Rust's standard #[test] attribute for test parametrization.

A test function can have any number of parameters which can have anonymouse types that will be filled in by the attribute based it's arguments.

Make sure attribute is applied before the standard Rust #[test] attribute or you will get functions used as tests can not have any arguments error. You can also use test_args attribute instead which appends the #[test] automatically.

Test case

Macro emits a new standard Rust test for each named argument set (also called a case) by suffixing function name with case name.

Cases are seperated by ; and need to have unique names for particular test function. Each case needs argument list seperated by , that consumes equal number of function parameters when generating the actual test function.

To mark a case as one that should panic, add a suffix with a slice of expected message after !

Syntax for a case is <case-name>: <arg1>, <arg2> ... <argN> [! "<message slice>"];

Cartesian product

You can apply mutiple test macros to a single function with individual macro cases consuming only a subset of function parameters. This forms a cartesian product of cases from each macro instance. It is import that all cartesian products consume all parameters or you will end up with a test function with nonzero parameters which is not supported by Rust built in test macro.

Example

The following example have two cases named char_a and char_b in first attribute and offset_0 and offset_1 in the second which combines into four tests:


#[args(
    char_a: 97, 'a';
    char_b: 98, 'b';
)]
#[args(
    offset_0: 0;
    offset_1: 1 ! "code incorrect";
)]
#[test]
fn parametrized_test_for(code:_, letter:_, offset:_) {
    assert_eq!(code + offset, letter as u8, "Letter code incorrect");
}

Should produce:

test parametrized_test_for__char_a__offset_0 ... ok
test parametrized_test_for__char_b__offset_0 ... ok
test parametrized_test_for__char_a__offset_1 ... ok
test parametrized_test_for__char_b__offset_1 ... ok