How to create parameterized tests in Rust to check multiple input data?
Hello colleagues! I recently started learning Rust and writing tests for my project. I have a function that calculates the factorial of a number:
```rust
fn factorial(n: u64) -> u64 {
match n {
0 | 1 => 1,
_ => n * factorial(n - 1),
}
}
```
I need to check its operation for different input values: 0, 1, 5, 10, etc. Currently I write a separate test for each case:
```rust
#[test]
fn test_factorial_0() {
assert_eq!(factorial(0), 1);
}
#[test]
fn test_factorial_1() {
assert_eq!(factorial(1), 1);
}
#[test]
fn test_factorial_5() {
assert_eq!(factorial(5), 120);
}
```
This is very inconvenient when you need to check dozens or hundreds of cases. In other languages (for example, in Python with `pytest.mark.parametrize`) I'm used to writing one parameterized
No answers yet.
Log in to leave an answer
Blogs
Working with Dates and Time in C++: A Complete Guide