#test-cases #junit #parametrized #unit-testing #test-macro #test

dev parameterized

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

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.0 Nov 9, 2019

#86 in Testing

Download history 520/week @ 2023-12-23 565/week @ 2023-12-30 841/week @ 2024-01-06 950/week @ 2024-01-13 1720/week @ 2024-01-20 529/week @ 2024-01-27 603/week @ 2024-02-03 1288/week @ 2024-02-10 867/week @ 2024-02-17 596/week @ 2024-02-24 1020/week @ 2024-03-02 815/week @ 2024-03-09 758/week @ 2024-03-16 853/week @ 2024-03-23 844/week @ 2024-03-30 740/week @ 2024-04-06

3,345 downloads per month
Used in 24 crates (21 directly)

MIT/Apache

17KB
106 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.

If you consider using parameterized, you can also check out Yare which is a variation on parameterized, which pivots the parameters, so you can define your own identifier for cases. Alternatively, there is Sif where each case can be defined by a separate #[case(...)] attribute.

Example:

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

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)
    }
}

Custom test attributes (e.g. tokio::test)

By default, the code generation step of the parameterized attribute will generate test cases marked with a #[test] attribute. For example, for the parameterized test case add5 below, the following code would be generated:

Parameterized test case

use parameterized::parameterized;

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

Generated code

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

    #[test]
    fn case_0() {
        assert_eq!(0 + 5, 5);
    }

    #[test]
    fn case_1() {
        assert_eq!(1 + 5, 6);
    }
}

However, sometimes a different test macro is desired, for example with #[tokio::test]. To let #[parameterized] use a user specified test macro, you may add the #[parameterized_macro(...)] attribute after a #[parameterized] attribute. Since we use #[tokio::test] in this example, we also add the async item to the function signature (but this is of course not mandatory for other macros).

Parameterized test case with #[parameterized_macro(...)]

#[parameterized(input = {
    0, 1
}, expected = {
    5, 6
})]
#[parameterized_macro(tokio::test)]
async fn add5(input: u32, expected: u32) {
    assert_eq!(input + 5, expected);
}

Gotchas:

  • The #[parameterized_macro(...)] must always be specified after a #[parameterized(...)] attribute
  • For now, only one #[parameterized_macro(...)] attribute per parameterized test function is supported.
  • While you can rename the parameterized attribute using import renaming ( e.g. use parameterized::parameterized as pm), the parameterized_macro attribute cannot be renamed, since it's not actually defined as a separate macro. Instead, the parameterized parses this attribute as well.

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

~1.2–1.7MB
~33K SLoC