#string

macro parameterized-macro

Attribute macro crate for parameterized tests

9 releases (4 stable)

2.0.0 Mar 5, 2024
1.1.0 Oct 12, 2023
1.0.1 Nov 9, 2022
1.0.0 May 2, 2022
0.1.1 Nov 14, 2019

#863 in Testing

Download history 1745/week @ 2024-08-26 1208/week @ 2024-09-02 1255/week @ 2024-09-09 1201/week @ 2024-09-16 1337/week @ 2024-09-23 1273/week @ 2024-09-30 1873/week @ 2024-10-07 2032/week @ 2024-10-14 1746/week @ 2024-10-21 1441/week @ 2024-10-28 1745/week @ 2024-11-04 1473/week @ 2024-11-11 1661/week @ 2024-11-18 2191/week @ 2024-11-25 1905/week @ 2024-12-02 2056/week @ 2024-12-09

7,860 downloads per month
Used in 30 crates (via parameterized)

MIT/Apache

16KB
281 lines

High level overview of test case generation

An example

With this macro we generate the following code (1) from the given parameterized test definition (0):

parameterized test definition:

// 0

#[parameterized(a = {1, 2}, b = { "wanderlust", "wanderer" })]
fn my_test(a: i32, b: &str) {
    assert!(a > 0 && b.starts_with("w"))
}

generated test cases:

// 1

#[cfg(test)]
mod my_test {
    #[test]
    fn case_0() {
        let a: i32 = 1;
        let b: &str = "wanderlust";
        assert!(a > 0 && b.starts_with("w"))
    }

    #[test]
    fn case_1() {
        let a: i32 = 2;
        let b: &str = "wanderer";
        assert!(a > 0 && b.starts_with("w"))
    }
}

More examples can be found in the expand crate, and the tests.

notes:

  • The function name in (1) is the same as the module name in (0)

  • Note that arguments are not limited to primitives; they can be any expression (assuming:)

  • In a parameterized test case, the input arguments (which are expressions) specified in the attribute should evaluate to the same type as their identically named companions in the function signature.

  • Tests executed from the workspace crate should be run individually, e.g. (cargo test --package parameterized-macro --test tests individual_cases -- --exact). Otherwise, if just cargo test is used, some generated test cases will run in an incorrect context setting.

Dependencies

~1.1–1.7MB
~32K SLoC