[−][src]Attribute Macro ntest::test_case
#[test_case]
Test cases can be used to have multiple inputs for a given function.
With the #[test_case]
attribute multiple tests will be generated using the
Procedural Macros
capabilities of rust.
The function input can be of type int
, bool
, or str
.
Please note that rust functions can only contain alphanumeric characters and '' signs.
Special characters will be escaped using a meaning full replacement (for example #
will be replaced with _hash
),
or as a default the '' sign.
WARNING!
It is currently not possible to have negative numbers as macro input. For example
this #[test_case(-13)]
will not work.
A function annoteded with a #[test_case]
attribute will be split into multiple rust functions annoteded with the #[test]
attribute.
Examples
Example with a single argument
#[test_case(13)] #[test_case(42)] fn one_arg(x: u32) { assert!(x == 13 || x == 42) }
The test cases above will be parsed at compile time and two rust test functions will be generated instead:
#[test] fn one_arg_13() { x = 13; assert!(x == 13 || x == 42) } #[test] fn one_arg_42() { x = 42; assert!(x == 13 || x == 42) }
Example with multiple arguments:
#[test_case(true, "true", 1)] fn test_mix(x: bool, y: &str, z: u16) { assert!(x); assert_eq!(y, "true"); assert_eq!(z, 1); }
Example with name attribute:
#[test_case(42, name="my_fancy_test")] fn with_name(x: u32) { assert_eq!(x, 42) }
Example with rust test attributes.
All attributes after a test case will be appended after the generated #[test]
attribute.
For example the following test cases...
#[test_case(18)] #[ignore] #[test_case(15)] #[should_panic(expected = "I am panicing")] fn attributes_test_case(x: u32) { panic!("I am panicing"); }
... will be compiled to these two tests. One gets ignored and the other suceeds:
#[test] #[ignore] fn attributes_test_case_18 { let x = 18; panic!("I am panicing"); } #[test] #[should_panic(expected = "I am panicing")] fn attributes_test_case_15() { let x = 15; panic!("I am panicing"); }