Function test_case_derive::test_case [] [src]

pub fn test_case(attr: TokenStream, input: TokenStream) -> TokenStream

Generates tests for given set of data

In general, test case consists of four elements:

  1. (Required) Arguments passed to test body
  2. (Optional) Expected result
  3. (Optional) Test case name
  4. (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
}