#parameterized #parametrized #test #unit-test #junit

dev parameterized

Procedural macro which brings a compact parameterized testing implementation to Rust (inspired by JUnit @ParameterizedTest)

5 unstable releases

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

#25 in Testing

Download history 1005/week @ 2021-07-02 717/week @ 2021-07-09 712/week @ 2021-07-16 882/week @ 2021-07-23 1244/week @ 2021-07-30 790/week @ 2021-08-06 1066/week @ 2021-08-13 752/week @ 2021-08-20 597/week @ 2021-08-27 989/week @ 2021-09-03 1121/week @ 2021-09-10 763/week @ 2021-09-17 1778/week @ 2021-09-24 1385/week @ 2021-10-01 1447/week @ 2021-10-08 1094/week @ 2021-10-15

3,699 downloads per month
Used in 20 crates (17 directly)

MIT/Apache

20KB
204 lines

parameterized

Procedural macro based parameterized testing library. Useful, when you want to run a test case with many different input sets.

When defining a parameterized test case, the #[parameterized(...)] attribute should be used instead of #[test].

This crate was inspired by JUnit @ParameterizedTest.

Examples:

Additional examples can be found at the parameterized-examples repository, and in the tests folder.


Example: Fruits

enum Fruit {
    Apple,
    Bramble(BrambleFruit),
    Pear,
}

trait NameOf {
    fn name_of(&self) -> &str;
}

impl NameOf for Fruit {
    fn name_of(&self) -> &str {
        match self {
            Fruit::Apple => "apple",
            Fruit::Bramble(fruit) => fruit.name_of(),
            Fruit::Pear => "pear",
        }
    }
}

enum BrambleFruit {
    Blackberry,
}

impl NameOf for BrambleFruit {
    fn name_of(&self) -> &str {
        match self {
            BrambleFruit::Blackberry => "blackberry",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use parameterized::parameterized;


    #[parameterized(fruit = {
        Fruit::Apple, Fruit::Pear, Fruit::Bramble(BrambleFruit::Blackberry)
    }, name = {
        "apple", "pear", "blackberry"
    })]
    fn a_fruity_test(fruit: Fruit, name: &str) {
        assert_eq!(fruit.name_of(), name)
    }
}

Example: Add5

fn add5<T: Into<u32>>(component: T) -> u32 {
    component.into() + 5
}

#[cfg(test)]
mod tests {
    use super::*;
    use parameterized::parameterized;

    ide!();

    #[parameterized(input = {
        0, 1, 2
    }, expected = {
        5, 6, 7
    })]
    fn test_add5(input: u16, expected: u32) {
        assert_eq!(add5(input), expected);
    }
}

Imports

If you prefer not to import this library (with use parameterized::parameterized;) in every test module, you can put the following snippet at the top of your crate root:

#[cfg(test)]
#[macro_use]
extern crate parameterized;

IDE 'run test' intent

IntelliJ IDEA recognizes test cases and provides context menus which allow you to run tests within a certain scope (such as a module or a single test case). For example, in IntelliJ you can usually run individual test cases by clicking the ▶ icon in the gutter. Unfortunately, attribute macros are currently not expanded by intellij-rust. This means that the IDE will not recognize test cases generated as a result of attribute macros (such as the parameterized macro published by this crate).

A workaround can be found below (if you have a better solution, please feel free to open an issue; thank you in advance!)

fn squared(input: i8) -> i8 {
  input * input  
}

#[cfg(test)]
mod tests {
    use super::*;

    use parameterized::parameterized as pm;
    use parameterized::ide;
        
    mod squared_tests { // <--
        use super::*;

        ide!(); // <--
    
        #[pm(input = {
            -2, -1, 0, 1, 2
        }, expected = {
            4, 1, 0, 1, 4
        })]
        fn test_squared(input: i8, output: i8) {
            assert_eq(squared(input), output);
        }
    }
}

Here we created an empty test case (using the ide!() macro) which will mark the surrounding module as 'containing test cases'. In the gutter you will find the ▶ icon next to the module. This allows you to run test cases per module.

Note: intellij-rust does expand declarative macro's (with the new macro engine which can be selected in the 'settings' menu), such as this ide! macro.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~345–770KB
~19K SLoC