Question
`writeln!(std::io::stdout().lock(), "")` cannot be captured by cargo test
I am trying to implement a logger in my multi-threading program. So I tried using a std::io::stdout
to gain a StdoutLock to ensure atomicity. But later I found in that way, all the log write to stdout cannot be capture when cargo test
.
I wrote a demo of this:
use std::io::Write as _;
pub fn print() {
println!("Hello, world! (print)");
}
pub fn write() {
let mut handle = std::io::stdout().lock();
writeln!(&mut handle, "Hello, world! (write)").unwrap();
handle.flush().unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_print() {
print();
}
#[test]
fn test_write() {
write();
}
}
When running cargo test
, it prints:
$ cargo test --lib
running 2 tests
Hello, world! (write)
test tests::test_print ... ok
test tests::test_write ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
I wonder how to avoid "Hello, world! (write)"
being printed when running tests.
4 51
4