How to use test-case crate for parameterized tests

Use the test-case crate to run a single test function with multiple argument sets via the #[test_case] macro.

The test-case crate allows you to run a single test function multiple times with different arguments using the #[test_case(...)] macro. Add the crate to your Cargo.toml, import the macro, and decorate your test function with the macro to define input parameters and expected results.

use test_case::test_case;

#[test_case(2, 2 => 4)]
#[test_case(5, 10 => 15)]
fn add(a: i32, b: i32) -> i32 {
    a + b
}
  1. Add the dependency to your Cargo.toml file. cargo add test-case

  2. Import the test_case macro in your test module. use test_case::test_case;

  3. Apply the #[test_case] attribute above your test function with arguments and expected output. #[test_case(2, 2 => 4)]

  4. Run the tests using the standard cargo test command. cargo test