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
}
-
Add the dependency to your
Cargo.tomlfile.cargo add test-case -
Import the
test_casemacro in your test module.use test_case::test_case; -
Apply the
#[test_case]attribute above your test function with arguments and expected output.#[test_case(2, 2 => 4)] -
Run the tests using the standard cargo test command.
cargo test