This crate provides #[test_case]
procedural macro attribute that generates multiple parametrized tests using one body with different input parameters.
A test is generated for each data set passed in test_case
attribute.
Under the hood, all test cases that share same body are grouped into mod
, giving clear and readable test results.
First of all you have to add this dependency to your Cargo.toml
:
toml
[dev-dependencies]
test-case-derive = "0.2.0"
Additionally you have to enable proc_macro
feature and include crate. You can do this globally by adding:
```
extern crate testcasederive; ```
to your lib.rs
or main.rs
file. Optionally you may enable proc macros only for tests:
```
extern crate testcasederive; ```
Don't forget that procedural macros are imported with use
statement:
use test_case_derive::test_case;
```
extern crate testcasederive;
use testcasederive::test_case;
fn multiplication_tests(x: i8, y: i8) { let actual = (x * y).abs();
assert_eq!(8, actual)
} ```
Output from cargo test
for this example:
``` $ cargo test
running 3 tests test multiplicationtests::whenbothoperandsarepossitive ... ok test multiplicationtests::whenbothoperandsarenegative ... ok test multiplicationtests::whenoperandsareswapped ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ```
If your only assertion is just assert_eq!
, you can pass the expectation as macro attribute using =>
syntax:
```
fn abs_tests(x: i8) -> i8 { if x > 0 { x } else { -x } } ```
Which is equivalent to
```
fn abs_tests(x: i8, expected: i8){ let actual = if x > 0 { x } else { -x };
assert_eq!(expected, actual); } ```
Attributes and expectation may be any expresion unless they contain =>
, e.g.
```
fn fancyaddition(x: Option
Note: in fact, =>
is not prohibited but the parser will always treat last =>
sign as beginning of expectation definition.
Test case names are optional. They are set using ::
followed by string literal at the end of macro attributes.
Example generated code:
``` mod fancyaddition { #[allow(unusedimports)] use super::*;
fn fancy_addition(x: Option<i8>, y: Option<i8>) -> i8 {
x.unwrap_or(0) + y.unwrap_or(0)
}
#[test]
fn treats_none_as_0() {
let expected = 0;
let actual = fancy_addition(None, None);
assert_eq!(expected, actual);
}
#[test]
fn some_2_some_3() {
let expected = 5;
let actual = fancy_addition(Some(2), Some(3));
assert_eq!(expected, actual);
}
#[test]
fn some_2_3_some_4() {
let expected = 2 + 3 + 4;
let actual = fancy_addition(Some(2 + 3), Some(4));
assert_eq!(expected, actual);
}
} ```
If test case name (passed using ::
syntax described above) contains word "inconclusive", generated test will be marked with #[ignore]
.
```
fn parses_input(input: &str) { // ... } ```
Generated code: ``` mod parses_input { // ...
#[test]
pub fn _42() {
// ...
}
#[test]
#[ignore]
pub fn inconclusive_parsing_letters_temporarily_doesn_t_work_but_it_s_ok() {
// ...
}
```
Note: word inconclusive
is only reserved in test name given after ::
.
All contributions and comments are more than welcome! Don't be afraid to open an issue or PR whenever you find a bug or have an idea to improve this crate.
MIT License
Copyright (c) 2017 Marcin Sas-SzymaĆski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.