Add rstest as a dev-dependency and use #[rstest] and #[case] macros to run parameterized tests in Rust.
Use rstest by adding it as a dev-dependency in Cargo.toml and annotating test functions with #[rstest] to define parameters.
// Cargo.toml
[dev-dependencies]
rstest = "0.23"
// src/lib.rs
use rstest::rstest;
#[rstest]
#[case(2, 4, 6)]
#[case(10, 20, 30)]
fn test_add(#[case] a: i32, #[case] b: i32, #[case] expected: i32) {
assert_eq!(a + b, expected);
}
Run the tests with cargo test.
rstest is a tool that helps you write tests that run multiple times with different data automatically. Instead of writing the same test function over and over for every number combination, you list the data once and the tool handles the repetition. It is like having a spreadsheet of test cases that your computer runs for you.