Rust 中测试函数是一个带有test属性注释的函数。属性attribute是关于Rust代码片段的元数据,derive就是一个典型的例子。为了将一个函数设置为测试函数,可以在函数名前加上一个#[test].当执行cargo test 时,Rust会构建一个测试程序来调用所有标记了该属性注释的函数。
对于一个成本的测试样例,得到的运行结果:
running7teststesttests::it_works...oktesttests::rectangle_large_can_hold_small...oktesttests::greeting_not_done...FAILEDtesttests::length_negative...oktesttests::rectangle_small_can_not_hold_large...oktesttests::return_result...FAILEDtesttests::width_negative...okfailures:----tests::greeting_not_donestdout----thread'tests::greeting_not_done' panicked at 'Greetingdidnotcontainthenamevalue:Hello,Cargo', src/lib.rs:40:9note: Run with `RUST_BACKTRACE=1` for a backtrace.---- tests::return_result stdout ----Error: "two add two does not equal three"thread 'tests::return_result' panicked at 'assertionfailed: `(left==right)`left: `1`,right: `0`:thetestreturnedaterminationvaluewithanon-zerostatuscode(1)whichindicatesafailure', src/libtest/lib.rs:337:5failures: tests::greeting_not_done tests::return_resulttest result: FAILED. 5 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out
# fn main() {}
pub fn greeting(name: &str) -> String {
format!("Hello {}!", name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn greeting_contains_name() {
let result = greeting("Carol");
assert!(result.contains("Carol"));
}
}
running 1 test
test tests::greeting_contains_name ... FAILED
failures:
---- tests::greeting_contains_name stdout ----
thread 'tests::greeting_contains_name' panicked at 'assertion failed:
result.contains("Carol")', src/lib.rs:12:8
note: Run with `RUST_BACKTRACE=1` for a backtrace.
failures:
tests::greeting_contains_name
#[test]
fn greeting_contains_name() {
let result = greeting("Carol");
assert!(
result.contains("Carol"),
"Greeting did not contain name, value was `{}`", result
);
}
#[test]
#[should_panic(expected = "The incorrect value of rectangle: 12.1, -5")]
fn width_negative() {
Rectangle::new(12.1, -5.);
}
impl Rectangle {
pub fn new(length: f64, width: f64) -> Rectangle {
if length <= 0. || width <= 0. {
panic!("The incorrect value of rectangle: {}, {}", length, width);
}
Rectangle {
length,
width,
}
}
}
mod tests {
#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
}
cargo test -- -- test-thread=1
cargo test -- -- nocapture
cargo test func_name
Compiling adder v0.1.0 (/home/inno/Learning-notes/RustInfo/projects/adder)
Finished dev [unoptimized + debuginfo] target(s) in 0.23s
Running target/debug/deps/adder-70257eb04d3ac84f
running 7 tests
test tests::greeting_not_done ... ok
test tests::it_works ... ok
test tests::rectangle_large_can_hold_small ... ok
test tests::rectangle_small_can_not_hold_large ... ok
test tests::length_negative ... ok
test tests::return_result ... ok
test tests::width_negative ... ok
test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target/debug/deps/integration_test-b8e9d862b2a5bae4
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out