Skip to main content

test_case

Attribute Macro test_case 

Source
#[test_case]
Expand description

Generates one #[test] per #[test_case(..)] line on a function.

Each attribute lists the positional arguments for one run, optionally followed by ; "label". The cases are gathered into a module named for the annotated function, so a labeled case is addressable as the_fn::the_label; an unlabeled case becomes the_fn::case_N. The original function stays callable inside that module as a helper.

When the function returns a value (the usual -> TestResult shape), each generated test wraps the call in failure context carrying the case label and the rendered arguments, so a failure names the case that produced it.

use test_better::prelude::*;

#[test_case("alice", 30 ; "common case")]
#[test_case("",      0  ; "empty name")]
fn validates_user(name: &str, age: u32) -> TestResult {
    check!(name.len()).satisfies(ge(age as usize))
}

A #[test_case] whose argument count does not match the function’s parameter count is a compile error, as is trailing junk after the label. Other attributes on the function (#[ignore], doc comments) are forwarded onto every generated test.