#parameterized #attribute #test #unit-test #junit

parameterized-macro

Attribute macro crate for parameterized tests

5 unstable releases

0.3.1 Jan 7, 2021
0.3.0 Dec 30, 2020
0.2.0 Jun 20, 2020
0.1.1 Nov 14, 2019
0.1.0 Nov 9, 2019

#160 in Testing

Download history 627/week @ 2021-06-29 924/week @ 2021-07-06 671/week @ 2021-07-13 738/week @ 2021-07-20 1135/week @ 2021-07-27 1052/week @ 2021-08-03 866/week @ 2021-08-10 852/week @ 2021-08-17 759/week @ 2021-08-24 653/week @ 2021-08-31 1205/week @ 2021-09-07 927/week @ 2021-09-14 1493/week @ 2021-09-21 1111/week @ 2021-09-28 1395/week @ 2021-10-05 1483/week @ 2021-10-12

3,706 downloads per month
Used in 21 crates (via parameterized)

MIT/Apache

12KB
159 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

~350–770KB
~19K SLoC