use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
fn read_file(path: &Path) {
let mut file = File::open(path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
println!("{contents}");
}
#[test]
fn test_read_file() {
let path = &Path::new("/etc/hosts");
println!("{path:?}");
read_file(path);
}
我以这种方式运行单元测试:
rustc --test app.rs; ./app
我也可以用这个来运行。
cargo test
我收到一条回复消息说测试通过了,但是屏幕上却没有显示
println!
。为什么呢?