Rust编写自动化测试一、编写和运行测试测试(函数)测试:函数验证非测试代码的功能是否和预期一致测试函数体(通常)执行的3个操作:准备数据/状态运行被测试的代码断言(Assert)结果解剖测试函数测试函数需要使用test属性(attribute)进行标注
使用 cargo test 命令运行所有测试函数
当使用 cargo 创建 library 项目的时候,会生成一个 test module,里面有一个test 函数
~/rust
➜ cargo new adder --lib
Created library `adder` package
~/rust
➜ cd adder
adder on master [?] via 🦀 1.67.1
➜ code .
adder on master [?] via 🦀 1.67.1 took 2.2s
➜
~/rust
➜ cargo new adder --lib
Created library `adder` package
~/rust
➜ cd adder
adder on master [?] via 🦀 1.67.1
➜ code .
adder on master [?] via 🦀 1.67.1 took 2.2s
➜
lib.rs 文件
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exploration() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn another() {
panic!("Make this test fail")
}
}
运行
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1 took 3.0s
➜ cargo test
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/adder)
Finished test [unoptimized + debuginfo] target(s) in 0.16s
Running unittests src/lib.rs (target/debug/deps/adder-6058f7b13179a51e)
running 2 tests
test tests::exploration ... ok
test tests::another ... FAILED
failures:
---- tests::another stdout ----
thread 'tests::another' panicked at 'Make this test fail', src/lib.rs:17:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::another
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
#[derive(Debug)]
pub struct Rectangle {
length: u32,
width: u32,
}
impl Rectangle {
pub fn can_hold(&self, other: &Rectangle) -> bool {
self.length > other.length && self.width > other.width
}
}
#[cfg(test)]
mod tests {
use super::*
#[test]
fn larger_can_hold_smaller() {
let larger = Rectangle {
length: 8,
width: 7,
};
let smaller = Rectangle {
length: 5,
width: 1,
};
assert!(larger.can_hold(&smaller));
}
#[test]
fn samller_cannot_hold_larger() {
let larger = Rectangle {
length: 8,
width: 7,
};
let smaller = Rectangle {
length: 5,
width: 1,
};
assert!(!smaller.can_hold(&larger));
}
}
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_adds_two() {
// assert_eq!(4, add_two(2));
assert_ne!(5, add_two(2));
}
}
pub fn greeting(name: &str) -> String {
//format!("Hello {}!", name)
format!("Hello!")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn greetings_contain_name() {
let result = greeting("Carol");
// assert!(result.contains("Carol"));
assert!(
result.contains("Carol"),
"Greeting didn't contain name, value was '{}'", result
);
}
}
pub struct Guess {
value: u32,
}
impl Guess {
pub fn new(value: u32) -> Guess {
if value < 1 || value > 100 {
panic!("Guess value must be between 1 and 100, got {}.", value)
}
Guess {value}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn greater_than_100() {
Guess::new(200);
}
}
pub struct Guess {
value: u32,
}
impl Guess {
pub fn new(value: u32) -> Guess {
if value < 1 {
panic!("Guess value must be greater than or equal to 1, got {}.", value)
} else if value > 100 {
panic!("Guess value must be less than or equal to 100, got {}.", value)
}
Guess {value}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic(expected = "Guess value must be less than or equal to 100")]
fn greater_than_100() {
Guess::new(200);
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
}
fn prints_and_returns_10(a: i32) -> i32 {
println!("I got the value {}", a);
10
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn this_test_will_pass() {
let value = prints_and_returns_10(4);
assert_eq!(10, value);
}
#[test]
fn this_test_will_fail() {
let value = prints_and_returns_10(8);
assert_eq!(5, value);
}
}
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_two_and_two() {
assert_eq!(4, add_two(2));
}
#[test]
fn add_three_and_two() {
assert_eq!(5, add_two(3));
}
#[test]
fn one_hundred() {
assert_eq!(102, add_two(100));
}
}
cargo test one_hundred
cargo test add
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(4, 2 + 2);
}
#[test]
#[ignore]
fn expensive_test() {
assert_eq!(5, 1 + 1 + 1 + 1 + 1);
}
}
cargo test -- --ignored
单元测试
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(4, 2 + 2);
}
}
pub fn add_two(a: i32) -> i32 {
internal_adder(a, 2)
}
fn internal_adder(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(4, internal_adder(2, 2));
}
}
src/lib.rs 文件
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exploration() {
let result = add(2, 2);
assert_eq!(result, 4);
}
// #[test]
// fn another() {
// panic!("Make this test fail")
// }
}
tests/integration_test.rs 文件
use adder;
#[test]
fn it_add() {
assert_eq!(5, adder::add(2, 3));
}
运行
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo test
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/adder)
Finished test [unoptimized + debuginfo] target(s) in 0.11s
Running unittests src/lib.rs (target/debug/deps/adder-6058f7b13179a51e)
running 1 test
test tests::exploration ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-461b916f2718e782)
running 1 test
test it_add ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
tests/another_integration_tests.rs 文件
use adder;
#[test]
fn it_adds2() {
assert_eq!(7, adder::add(3,4));
}
运行
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo test --test integration_test
Finished test [unoptimized + debuginfo] target(s) in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-461b916f2718e782)
running 1 test
test it_add ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo test --test another_integration_tests
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/adder)
Finished test [unoptimized + debuginfo] target(s) in 0.11s
Running tests/another_integration_tests.rs (target/debug/deps/another_integration_tests-0a89cbf68d5b375f)
running 1 test
test it_adds2 ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
adder on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
├── target
│ ├── CACHEDIR.TAG
│ ├── debug
│ └── tmp
└── tests
├── another_integration_tests.rs
├── common
│ └── mod.rs
└── integration_test.rs
27 directories, 205 files
tests/common/mod.rs 文件
pub fn setup() {}
tests/another_integration_tests.rs 文件
use adder;
mod common;
#[test]
fn it_adds2() {
common::setup();
assert_eq!(7, adder::add(3,4));
}
tests/integration_test.rs 文件
use adder;
mod common;
#[test]
fn it_add() {
common::setup();
assert_eq!(5, adder::add(2, 3));
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!