#[test_case]
Expand description
Generates tests for given set of data
In general, test case consists of four elements:
- (Required) Arguments passed to test body
- (Optional) Expected result
- (Optional) Test case name
- (Required) Test body
When expected result is provided, it is compared against the actual value generated with test body using assert_eq!
.
Test cases that don’t provide expected result should contain custom assertions inside test body.
Examples
- Without result and name
#[test_case(5)]
#[test_case(10)]
fn is_positive(x: i8) {
assert!(x > 0)
}
- With name, without result
#[test_case(1 ; "little number")]
#[test_case(100 ; "big number")]
#[test_case(5)] // some tests may use default name generated from arguments list
fn is_positive(x: i8) {
assert!(x > 0)
}
- With result, without name
#[test_case(1, 2 => 3)]
#[test_case(-1, -2 => -3)]
fn addition(x: i8, y: i8) -> i8 {
x + y
}
- With result and name
#[test_case(1, 2 => 3 ; "both numbers possitive")]
#[test_case(-1, -2 => -3 ; "both numbers negative")]
fn addition(x: i8, y: i8) -> i8 {
x + y
}