#[test]
Expand description
test
attribute is applied to a test function.
You must explicitly use rustest::test
(or mark the function with #[rustest::test]
).
If not, rust will detect a ambigous name for test
and refuse to compile.
use rustest::*;
#[test]
fn the_test() {
}
#[main]
fn main() {}
use rustest::{test,*};
#[test]
fn the_test() {
}
#[main]
fn main() {}
use rustest::*;
#[rustest::test]
fn the_test() {
}
#[main]
fn main() {}
Your annotated function’s arguments can be a fixture with [fixture]
s
or a parameter.
Your test function can use fixtures as argument and can return results.
They can also be marked by #[xfail]
attribute.
In your test function you can:
Additional Attributes:
- Function Attributes:
#[xfail]
Expect the test to fail
§Injecting Fixtures
The simplest case is write a test that can be injected with
[fixture]
s. You can just declare all used fixtures by passing
them as a function’s arguments.
use rustest::{test, *};
#[fixture]
fn MyFixture() -> i32 { 42 }
#[test]
fn the_test(v: MyFixture) {
assert_eq!(42, *v)
}
#[main]
fn main() {}
See fixture macro documentation for full options on fixtures declaration.
§Parametrized Values
As for fixtures, you can directly provide a list of params to avoid declaring a fixture.
use rustest::{test, *};
#[test(params:u32=[1,2,5])]
fn test(param: Param) {
assert!([1,2,5].contains(¶m))
}
#[main]
fn main() {}
rustest
will produce 3 independent tests and not just one that
check every case. Every test can fail independently and cargo test
will give follow output:
running 5 tests
test test::1 ... ok
test test::2 ... ok
test test::5 ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Param value can be destructured:
use rustest::test;
#[test(params:(u32, u32)=[
(0, 0),
(1, 1),
(2, 1),
(3, 2),
(4, 3),
(5, 5),
(6, 8),
])]
fn fibonacci_test(Param((input, expected)): Param) {
assert_eq!(expected, fibonacci(input))
}
fn fibonacci(input: u32) -> u32 {
match input {
0 => 0,
1 => 1,
n => fibonacci(n - 2) + fibonacci(n - 1)
}
}
#[rustest::main]
fn main() {}